【问题标题】:Scrapy / Use Scrapy Selenium for the first request-page?Scrapy / 对第一个请求页面使用 Scrapy Selenium?
【发布时间】:2022-01-08 09:30:19
【问题描述】:

我有一个使用scrapy_selenium 的正在运行的解决方案,用于带有javascript 加载的站点。 正如您在下面的代码中看到的那样,在使用 parseDetails 生成 detailPage 时使用了 SeleniumRequest -

但是当我需要 SeleniumRequest 在我的主页(而不仅仅是像下面这样的详细信息页面)时,我该怎么办?

在这种情况下如何使用 SeleniumRequest?

import scrapy
from scrapy_selenium import SeleniumRequest

class ZoosSpider(scrapy.Spider):
  name = 'zoos'
  allowed_domains = ['www.tripadvisor.co.uk']
  start_urls = [
                "https://www.tripadvisor.co.uk/Attractions-g186216-Activities-c53-a_allAttractions.true-United_Kingdom.html"
                ]  
  existList = []  

  def parse(self, response):
    tmpSEC = response.xpath("//section[@data-automation='AppPresentation_SingleFlexCardSection']")
    for elem in tmpSEC:
      link = response.urljoin(elem.xpath(".//a/@href").get())   
      yield SeleniumRequest(
        url=link, 
        wait_time= 10,        
        callback=self.parseDetails)  

  def parseDetails(self, response):
    tmpName = response.xpath("//h1[@data-automation='mainH1']/text()").get()  
    tmpLink = response.xpath("//div[@class='Lvkmj']/a/@href").getall()    
    tmpURL = tmpTelnr = tmpMail = "N/A"

    yield {
      "Name": tmpName,
      "URL": tmpURL,
    }

【问题讨论】:

    标签: python selenium web-scraping scrapy scrapy-selenium


    【解决方案1】:

    您可以使用自己的函数start_requests() 启动第一个请求。

    class ZoosSpider(scrapy.Spider):
    
        def start_requests(self):
            for link in self.start_urls:
                yield SeleniumRequest(
                    url=link, 
                    wait_time= 10,        
                    callback=self.parse)
    

    参见文档中的第一点:Spider

    The first requests to perform are obtained by calling the start_requests() method  
    which (by default) generates Request for the URLs specified in the start_urls 
    and the parse method as callback function for the Requests.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-08
      • 1970-01-01
      • 1970-01-01
      • 2015-04-02
      • 2018-11-15
      • 1970-01-01
      • 2019-02-07
      • 1970-01-01
      相关资源
      最近更新 更多