【问题标题】:Struggling with Scrapy pagination挣扎于 Scrapy 分页
【发布时间】:2020-03-14 22:41:01
【问题描述】:

目前有一些科学怪人代码(由 Beautifulsoup 和 Scrapy 部分组成)似乎在从第 1 页 url 读取信息方面发挥了作用。分页问题解决后,应尝试重做 Scrapy 中的所有内容。

那么代码的作用是什么:

  1. 阅读所有子类别(BeautifulSoup 部分)

其余都是 Scrapy 代码部分

  1. 使用上述网址读取子子类别。

  2. 提取最后一个页码并遍历上述网址。

  3. 从上述网址中提取必要的产品信息。

除了第 3 部分之外的所有内容似乎都有效。

尝试使用以下代码提取最后页码,但不知道如何将其集成到主代码中

def parse_paging(self, response):
        try:
            for next_page in ('?pn=1' + response.xpath('//ul[@class="pagination pull-left"]/noscript/a/text()').extract()[-1]):
                print(next_page)
#                yield scrapy.Request(url=response.urljoin(next_page))
        except:
            pass

以下是主要代码。

import requests
from bs4 import BeautifulSoup
import pandas as pd
import scrapy
from scrapy.crawler import CrawlerProcess

category_list = []
sub_category_url = []

root_url = 'https://uk.rs-online.com/web'
page = requests.get(root_url)
soup = BeautifulSoup(page.content, 'html.parser')
cat_up = [a.find_all('a') for a in soup.find_all('div',class_='horizontalMenu sectionUp')]
category_up = [item for sublist in cat_up for item in sublist]
cat_down = [a.find_all('a') for a in soup.find_all('div',class_='horizontalMenu sectionDown')]
category_down = [item for sublist in cat_down for item in sublist]
for c_up in category_up:
    sub_category_url.append('https://uk.rs-online.com' + c_up['href'])
for c_down in category_down:
    sub_category_url.append('https://uk.rs-online.com' + c_down['href'])
#   print(k)


class subcategories(scrapy.Spider):
    name = 'subcategories'

    def start_requests(self):
        urls = sub_category_url
        for url in urls:
            yield scrapy.Request(url=url, callback=self.parse)
    def parse(self, response):
        products = response.css('div.card.js-title a::href').extract() #xpath("//div[contains(@class, 'js-tile')]/a/@href").
        for p in products:
            url = urljoin(response.url, p)
            yield scrapy.Request(url, callback=self.parse_product)
    def parse_product(self, response):
        for quote in response.css('tr.resultRow'):
            yield {
                'product': quote.css('div.row.margin-bottom a::text').getall(),
                'stock_no': quote.css('div.stock-no-label a::text').getall(),
                'brand': quote.css('div.row a::text').getall(),
                'price': quote.css('div.col-xs-12.price.text-left span::text').getall(),
                'uom': quote.css('div.col-xs-12.pack.text-left span::text').getall(),
            }
process = CrawlerProcess()
process.crawl(subcategories)
process.start()

如果您能提供有关如何处理上述问题的任何提示,将不胜感激。

如果您有任何问题,请告诉我。

【问题讨论】:

  • 为什么代码里有两个def parse(self, response)
  • 道歉。应该是 parse_product。已编辑代码

标签: web-scraping pagination scrapy


【解决方案1】:

我建议您使用此方法提取下一页编号 然后使用这个数字构造下一页 url。

next_page_number = response.css('.nextPage::attr(ng-click)').re_first('\d+')

【讨论】:

  • 这个有帮助。谢谢
  • 您会推荐任何优化更改吗?网站上有大约 100 万种产品,并且 cmets 运行良好的几个小时。我知道以上还远非完美,所以可能有一些事情可以做来提高性能?
  • 这取决于很多东西,例如:网站架构、您的带宽、网站反机器人设置(例如最大连接数、验证码等)、代理池(如果需要)等。首先尝试玩与线程数量。关于爬虫架构 - 最好的方法是一次打开多个产品类别,然后按照每个产品类别进行分页。
猜你喜欢
  • 1970-01-01
  • 2023-04-06
  • 2014-08-30
  • 1970-01-01
  • 1970-01-01
  • 2015-08-24
  • 2021-08-05
  • 2012-01-06
  • 2012-12-29
相关资源
最近更新 更多