【问题标题】:Dealing with pagination when using scrapy-selenium (POST request)使用scrapy-selenium时处理分页(POST请求)
【发布时间】:2020-09-09 15:02:48
【问题描述】:

我正在尝试抓取以下网站:

https://www.getwines.com/main.asp?request=search&type=w&s1=s9818865857&fbclid=IwAR3yF9x1X7sdPYgsfl4vF1oNF7GNoF1pSov4lwJLEeeTYFGevBTfRKOPBmo

我成功地抓取了第一页,但我无法进入下一页。这有两个原因:

  1. 检查next_page 按钮时,我没有得到相对或绝对值 网址。相反,我得到了JavaScript:getPage(2),我无法使用它来关注链接

  2. 下一页按钮链接可以通过(//table[@class='tbl_pagination']//a//@href)[11]访问时 在第一页,但从第 2 页开始,下一页按钮是第 12 项, 即(//table[@class='tbl_pagination']//a//@href)[12]

所以最终我的问题是,我如何有效地转到所有后续页面并抓取数据。

这可能很容易解决,但我是网络抓取的初学者,因此感谢任何反馈。请看下面我的代码。

感谢您的帮助。

**
import scrapy
from scrapy_selenium import SeleniumRequest
class WinesSpider(scrapy.Spider):
    name = 'wines'
  
    def start_requests(self):
        yield SeleniumRequest(
        url='https://www.getwines.com/category_Wine',
        wait_time=3,
        callback=self.parse
        )
    def parse(self, response):
        products = response.xpath("(//div[@class='layMain']//tbody)[5]/tr ")
        for product in products:
            yield {
                'product_name': 
                product.xpath(".//a[@class='Srch-producttitle']/text()").get(),
                'product_link': 
                product.xpath(".//a[@class='Srch-producttitle']/@href").get(),
                'product_actual_price': 
                product.xpath(".//td//td[3]//td/span[2]/text()").get(),
                'product_price_onsale': 
                product.xpath(".//td//td[3]//td/span[4]/text()").get()
            }
    #next_page = response.xpath("(//table[@class='tbl_pagination']//a//@href)[11]").get()
    #if next_page:
    #    absolute_url = f"'https://www.getwines.com/category_Wine"**

【问题讨论】:

    标签: javascript selenium post pagination scrapy


    【解决方案1】:

    请查看下方回答上述问题的代码。

    简而言之,我更改了代码的结构,现在它可以完美运行了。一些备注:

    1. 首先,将页面的所有内容保存在一个列表中
    2. 在 while-try 循环结束时使用“except NoSuchElementException”很重要 --> 在添加此之前,代码一直失败,因为它不知道到达最后一页后该做什么。李>
    3. 访问存储的链接(响应)的内容。

    总而言之,我认为在将 Selenium 与 Scrapy 集成时,以这种方式构建 Scrapy 代码效果很好。但是,由于我是 Web Scraping 的初学者,因此对于如何有效地将 Selenium 与 Scrapy 集成的任何其他反馈将不胜感激。

    # -*- coding: utf-8 -*-
    import scrapy
    from scrapy import Selector
    from scrapy_selenium import SeleniumRequest
    from selenium.common.exceptions import NoSuchElementException
    
    class WinesSpider(scrapy.Spider):
        name = 'wines'
    
        responses = []
    
        def start_requests(self):
            yield SeleniumRequest(
                url='https://www.getwines.com/category_Wine',
                callback=self.parse
            )
    
        def parse(self, response):
            driver = response.meta['driver']
            intial_page = driver.page_source
            self.responses.append(intial_page)
            found = True
            while found:
                try:
                    next_page = driver.find_element_by_xpath("//b[text()= '>>']/parent::a")
                    href = next_page.get_attribute('href')
                    driver.execute_script(href)
                    driver.implicitly_wait(2)
                    self.responses.append(driver.page_source)
            
                except NoSuchElementException:
                    break
    
            for resp in self.responses:
                r = Selector(text=resp)
                products = r.xpath("(//div[@class='layMain']//tbody)[5]/tr")
                for product in products:
                    yield {
                        'product_name':
                        product.xpath(".//a[@class='Srch-producttitle']/text()").get(),
                        'product_link':
                        product.xpath(".//a[@class='Srch-producttitle']/@href").get(),
                        'product_actual_price':
                        product.xpath(".//span[@class='RegularPrice']/text()").get(),
                        'product_price_onsale':
                        product.xpath(".//td//td[3]//td/span[4]/text()").get()
                    }
    

    【讨论】:

      猜你喜欢
      • 2015-07-23
      • 2022-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-28
      • 2014-04-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多