使用 Scrapy 通过无限滚动 Ajax 提取数据

Nic*_*aga 2 ajax jquery scrapy web-scraping infinite-scroll

我是 python 和 scrapy 的初学者。我需要帮助,我需要提取产品列表,但他的网站最后有一个“查看更多”产品,该产品使用 ajax 执行文本/html 请求并加载另一个包含新产品的 html。

import scrapy
from scrapy.http import Request


class ProdSpider(scrapy.Spider):
name = "easy"
allowed_domains = ["easy.com.ar"]
start_urls = ["https://www.easy.com.ar/webapp/wcs/stores/servlet/es/easyar/search/AjaxCatalogSearchResultContentView?searchTermScope=&searchType=1002&filterTerm=&orderBy=&maxPrice=&showResultsPage=true&langId=-5&sType=SimpleSearch&metaData=&pageSize=12&manufacturer=&resultCatEntryType=&catalogId=10051&pageView=image&searchTerm=&minPrice=&categoryId=39652&storeId=10151&beginIndex=12"]
beginIndex_index = 12

def parse(self, response):
    SECTION_SELECTOR = '.thumb-product'
    for soar in response.css(SECTION_SELECTOR):

             Link = 'div.dojoDndItem a ::attr(href)'
             # Marca = 'p.brand a ::text'
             Nombre = 'div.thumb-name a ::text'
             # Link = 'p.brand a ::attr(href)'
             # SKU = './/p[@class="sku"]/text()' #p.sku ::text'
             Price = './/span[@id="tarj-mas-edit"]/text()' #["0"].parentElement.innerText    .//span[@class="thumb-price-e"]/text()

             yield {
               'Link': soar.css(Link).extract_first(),
            # 'Marca': soar.css(Marca).extract_first(),
               'Nombre': soar.css(Nombre).re_first(r'\n\s*(.*)'), # Limpia espacios y caracteres especiales
            # 'Link': soar.css(Link).extract_first(),   
            # 'SKU': soar.xpath(SKU).re_first(r'SKU:\s*(.*)'),
               'Price': soar.xpath(Price).re_first(r'\n\s*(.*)'),
              }
    # here if no products are available , simply return, means exiting from
    # parse and ultimately stops the spider


    self.beginIndex_index += 12
    if beginIndex_index:
        yield Request(url="https://www.easy.com.ar/webapp/wcs/stores/servlet/es/easyar/search/AjaxCatalogSearchResultContentView?searchTermScope=&searchType=1002&filterTerm=&orderBy=&maxPrice=&showResultsPage=true&langId=-5&sType=SimpleSearch&metaData=&pageSize=12&manufacturer=&resultCatEntryType=&catalogId=10051&pageView=image&searchTerm=&minPrice=&categoryId=39652&storeId=10151&beginIndex=%s" % (self.beginIndex_index + 12),
                      callback=self.parse)
Run Code Online (Sandbox Code Playgroud)

我尝试使用上面的代码,但只捕获了 12 个产品。url 中唯一改变的参数是“beginIndex=12”,Y 希望对 url 求和 +12,直到产品列表结束。我被这个问题困住了!

谢谢!

Adr*_*uer 5

我建议您使用selenium,这样您就可以“单击”查看更多按钮并在蜘蛛中加载更多数据。这是蜘蛛的示例(我没有测试它,但这是总体思路):

import scrapy
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary


class ProdSpider(scrapy.Spider):
    name = "easy"
    allowed_domains = ["easy.com.ar"]
    start_urls = ["https://www.easy.com.ar/webapp/wcs/stores/servlet/es/easyar/search/aditivos-y-lubricantes"]

    def __init__(self):
        super(ProdSpider, self).__init__()
        binary = FirefoxBinary('C:/Program Files (x86)/Mozilla Firefox/firefox.exe')
        self.wb = webdriver.Firefox(firefox_binary=binary)

    def parse(self, response):
        self.wb.get(response.url)
        while True:
            view_more_button = self.wb.find_element_by_xpath('//*[@id="Search_Result_div"]/div[2]/div[9]/input')
            if not view_more_button:
                break
            view_more_button.click()
            #extract your data here...
Run Code Online (Sandbox Code Playgroud)