【问题标题】:Scrapy Selenim not extracting data from all start_urlsScrapy Selenim 没有从所有 start_urls 中提取数据
【发布时间】:2020-09-17 19:10:54
【问题描述】:

如果只列出一个 url,下面的代码将提取数据。如果我输入两个 url(如下所示),它只会从第一个 url 中提取数据 - 但是两次!知道如何克服这个问题吗?请注意,我删除了一些选择器以缩短代码。

import scrapy
from scrapy_selenium import SeleniumRequest
from selenium import webdriver

class Spider(scrapy.Spider):
    name = "match_summary"

    def start_requests(self):
        urls = [
            'https://www.flashscore.com/match/v5GmqsWa/#match-summary',
            'https://www.flashscore.com/match/Wju9nz58/#match-summary',]

        for url in urls:
            yield SeleniumRequest(url=url, callback=self.parse,dont_filter=True)

    def parse(self, response):
        for quote in response.css('div.detailMS__incidentRow'):
            yield {'Match': response.selector.xpath('//title/text()').getall(),
                   'Round': response.selector.xpath('/html/body/div[2]/div[1]/div[2]/div[1]/span[2]/a/text()').getall(),
                   'Date': response.selector.xpath('//*[@id="utime"]/text()').getall(),
                   'Time': quote.css('div.time-box::text').extract(),
                   'OverTime': quote.css('div.time-box-wide::text').extract()}

【问题讨论】:

  • 你能分享你在这个类中实际运行函数的代码吗?我试图查看您的问题,但我没有足够的知识来复制您提供的信息。
  • 我复制的代码是 Scrapy 项目(蜘蛛)的一部分,需要创建。然后通过命令行运行抓取(例如,“scrapy crawl match_summary”)。不知道 Scrapy 项目的哪个部分调用了这个函数,如果这就是你的意思的话。

标签: python selenium scrapy


【解决方案1】:

请注意:不要给你的班级命名 Spider ,它与 library's Spider class 冲突,可能会混淆你的脚本。

您是否尝试过使用start_urls 而不是start_requests 方法?

class MySpider(scrapy.Spider):
    name = 'match_summary'
    start_urls = [
        'https://www.flashscore.com/match/v5GmqsWa/#match-summary',
        'https://www.flashscore.com/match/Wju9nz58/#match-summary',]

    def parse(self, response):
        for quote in response.css('div.detailMS__incidentRow'):
        yield {'Match': response.selector.xpath('//title/text()').getall(),
               'Round': response.selector.xpath('/html/body/div[2]/div[1]/div[2]/div[1]/span[2]/a/text()').getall(),
               'Date': response.selector.xpath('//*[@id="utime"]/text()').getall(),
               'Time': quote.css('div.time-box::text').extract(),
               'OverTime': quote.css('div.time-box-wide::text').extract()}

【讨论】:

  • 页面需要通过SeleniumRequest打开,否则不起作用(不知道背后的web技术细节)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-13
  • 1970-01-01
  • 2019-03-10
  • 1970-01-01
  • 2015-02-03
  • 2018-07-15
  • 1970-01-01
相关资源
最近更新 更多