Python 使用BeautifulSoup和请求刮取多个分页链接

Python 使用BeautifulSoup和请求刮取多个分页链接,python,for-loop,web-scraping,beautifulsoup,screen-scraping,Python,For Loop,Web Scraping,Beautifulsoup,Screen Scraping,这里是Python初学者。我想把所有的产品都刮下来。我已经设法在一个给定的页面上刮取所有的产品,但是我在迭代所有分页的链接时遇到了问题 现在,我已经尝试用span class='page-list“隔离所有分页按钮,但即使这样也不起作用。理想情况下,我希望爬虫一直单击下一步,直到它在所有页面上刮掉所有产品。我如何才能做到这一点 非常感谢您的意见 from bs4 import BeautifulSoup import requests base_url = "http://www.dabs.

这里是Python初学者。我想把所有的产品都刮下来。我已经设法在一个给定的页面上刮取所有的产品,但是我在迭代所有分页的链接时遇到了问题

现在,我已经尝试用span class='page-list“隔离所有分页按钮,但即使这样也不起作用。理想情况下,我希望爬虫一直单击下一步,直到它在所有页面上刮掉所有产品。我如何才能做到这一点

非常感谢您的意见

from bs4 import BeautifulSoup

import requests

base_url = "http://www.dabs.com"
page_array = []

def get_pages():
    html = requests.get(base_url)
    soup = BeautifulSoup(html.content, "html.parser")

    page_list = soup.findAll('span', class="page-list")
    pages = page_list[0].findAll('a')

    for page in pages:
        page_array.append(page.get('href'))

def scrape_page(page):
    html = requests.get(base_url)
    soup = BeautifulSoup(html.content, "html.parser")
    Product_table = soup.findAll("table")
    Products = Product_table[0].findAll("tr")

    if len(soup.findAll('tr')) > 0:
        Products = Products[1:]

    for row in Products:
        cells = row.find_all('td')
        data = {
            'description' : cells[0].get_text(),
            'price' : cells[1].get_text()
        }
        print data

get_pages()
[scrape_page(base_url + page) for page in page_array]

他们的“下一页”按钮标题为“下一页”,您可以执行以下操作:

import requests
from bs4 import BeautifulSoup as bs

url = 'www.dabs.com/category/computing/11001/'
base_url = 'http://www.dabs.com'

r = requests.get(url)

soup = bs(r.text)
elm = soup.find('a', {'title': 'Next'})

next_page_link = base_url + elm['href']
希望有帮助