【问题标题】:Automate page scroll to down in Splash and Scrapy在 Splash 和 Scrapy 中自动向下滚动页面
【发布时间】:2021-06-08 23:15:41
【问题描述】:

我正在抓取一个对产品图片使用延迟加载的网站。

出于这个原因,我包含了scrapy-splash,以便javascript 也可以用splash 呈现,我可以提供wait 参数。以前我认为这是因为原始scrapy.Request 返回占位符图像而不是原始图像的时间。

我也尝试过等待 29.0 秒的参数,但我的爬虫仍然几乎没有得到 10 个项目(根据计算它应该带来 280 个项目)。我有一个项目管道,它检查项目中的图像是否为空,所以我 raise DropItem.

我不确定,但我也注意到它不仅仅是wait 的问题。当我向下滚动时,看起来图像被加载了。

我正在寻找的是一种在我的请求中自动滚动到底部行为的方法。

这是我的代码 蜘蛛

  def parse(self, response):
        categories = response.css('div.navigation-top-links a.uppercase::attr(href)').extract()
        for category in categories:
            link = urlparse.urljoin(self.start_urls[0], category)
            yield SplashRequest(link, callback=self.parse_products_listing, endpoint='render.html',
                                       args={'wait': 0.5})

管道

class ScraperPipeline(object):
    def process_item(self, item, spider):
        if not item['images']:
            raise DropItem

        return item

设置

IMAGES_STORE = '/scraper/images'
        
SPLASH_URL = 'http://172.22.0.2:8050'

DUPEFILTER_CLASS = 'scrapy_splash.SplashAwareDupeFilter'


ITEM_PIPELINES = {
   'scraper.pipelines.ScraperPipeline': 300,
    'scrapy.pipelines.images.ImagesPipeline': 1
}


DOWNLOADER_MIDDLEWARES = {
    'scrapy.downloadermiddleware.useragent.UserAgentMiddleware': None,
    'scrapy_splash.SplashMiddleware': 725,
    'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware': 810,
    # 'custom_middlewares.middleware.ProxyMiddleware': 210,
}

【问题讨论】:

    标签: scrapy scrapy-splash


    【解决方案1】:

    如果您打算使用 splash 这个答案应该会给您一些指导:https://stackoverflow.com/a/40366442/7926936

    您也可以在 DownloaderMiddleware 中使用 selenium,这是我的 Twitter scraper 示例,它将获取页面的前 200 条推文:

    from selenium import webdriver
    from scrapy.http import HtmlResponse
    from selenium.common.exceptions import TimeoutException
    from selenium.webdriver.support.ui import WebDriverWait
    
    
    class SeleniumMiddleware(object):
    
        def __init__(self):
            self.driver = webdriver.PhantomJS()
    
        def process_request(self, request, spider):
            self.driver.get(request.url)
            tweets = self.driver.find_elements_by_xpath("//li[@data-item-type='tweet']")
            while len(tweets) < 200:
                try:
                    self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
                    WebDriverWait(self.driver, 10).until(
                        lambda driver: new_posts(driver, len(tweets)))
                    tweets = self.driver.find_elements_by_xpath("//li[@data-item-type='tweet']")
                except TimeoutException:
                    break
            body = self.driver.page_source
            return HtmlResponse(self.driver.current_url, body=body, encoding='utf-8', request=request)
    
    
    def new_posts(driver, min_len):
        return len(driver.find_elements_by_xpath("//li[@data-item-type='tweet']")) > min_len
    

    在 while 循环中,我在每个循环中等待新推文,直到页面中加载了 200 条推文并且最长等待 10 秒。

    【讨论】:

      猜你喜欢
      • 2014-10-02
      • 1970-01-01
      • 2020-09-05
      • 2017-12-22
      • 2012-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多