【问题标题】:Scraping multiple paginated links with BeautifulSoup and Requests使用 BeautifulSoup 和 Requests 抓取多个分页链接
【发布时间】:2015-04-20 05:55:17
【问题描述】:

这里是 Python 初学者。我正在尝试从one category on dabs.com 抓取所有产品。我已经设法抓取给定页面上的所有产品,但是在遍历所有分页链接时遇到了麻烦。

现在,我尝试使用 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]

【问题讨论】:

    标签: python for-loop web-scraping beautifulsoup screen-scraping


    【解决方案1】:

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

    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']
    

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-24
      • 1970-01-01
      • 2014-12-17
      • 1970-01-01
      • 2021-11-02
      • 1970-01-01
      • 2015-10-23
      • 2017-12-09
      相关资源
      最近更新 更多