gpt4 book ai didi

python - Scrapy从多个页面中提取相同的数据

转载 作者:太空宇宙 更新时间:2023-11-03 16:17:24 25 4
gpt4 key购买 nike

这与我之前写的问题here有关。我正在尝试从同一域的多个页面中提取相同的数据。一个小解释,我试图从 main page 上的一堆不同的得分中提取进攻码数、失误等数据。 。从各个页面提取数据和生成 URL 一样工作正常,但是当我尝试让蜘蛛循环遍历所有页面时,没有返回任何内容。我浏览了人们提出的许多其他问题和文档,但我无法弄清楚什么不起作用。代码如下。感谢任何能够提前提供帮助的人。

import scrapy

from scrapy import Selector
from nflscraper.items import NflscraperItem

class NFLScraperSpider(scrapy.Spider):
name = "pfr"
allowed_domains = ['www.pro-football-reference.com/']
start_urls = [
"http://www.pro-football-reference.com/years/2015/games.htm"
#"http://www.pro-football-reference.com/boxscores/201510110tam.htm"
]

def parse(self,response):
for href in response.xpath('//a[contains(text(),"boxscore")]/@href'):
item = NflscraperItem()
url = response.urljoin(href.extract())
request = scrapy.Request(url, callback=self.parse_dir_contents)
request.meta['item'] = item
yield request

def parse_dir_contents(self,response):
item = response.meta['item']
# Code to pull out JS comment - https://stackoverflow.com/questions/38781357/pro-football-reference-team-stats-xpath/38781659#38781659
extracted_text = response.xpath('//div[@id="all_team_stats"]//comment()').extract()[0]
new_selector = Selector(text=extracted_text[4:-3].strip())
# Item population
item['home_score'] = response.xpath('//*[@id="content"]/table/tbody/tr[2]/td[last()]/text()').extract()[0].strip()
item['away_score'] = response.xpath('//*[@id="content"]/table/tbody/tr[1]/td[last()]/text()').extract()[0].strip()
item['home_oyds'] = new_selector.xpath('//*[@id="team_stats"]/tbody/tr[6]/td[2]/text()').extract()[0].strip()
item['away_oyds'] = new_selector.xpath('//*[@id="team_stats"]/tbody/tr[6]/td[1]/text()').extract()[0].strip()
item['home_dyds'] = item['away_oyds']
item['away_dyds'] = item['home_oyds']
item['home_turn'] = new_selector.xpath('//*[@id="team_stats"]/tbody/tr[8]/td[2]/text()').extract()[0].strip()
item['away_turn'] = new_selector.xpath('//*[@id="team_stats"]/tbody/tr[8]/td[1]/text()').extract()[0].strip()
yield item

最佳答案

您发出的后续请求将被过滤为场外,请修复您的allowed_domains 设置:

allowed_domains = ['pro-football-reference.com'] 

为我工作。

关于python - Scrapy从多个页面中提取相同的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38821027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com