【发布时间】:2020-10-03 21:00:39
【问题描述】:
我正在scrapy 上构建我的第一个蜘蛛,它旨在从博彩网站上抓取数据并返回参赛球队的名称和赔率。我正在使用 for 循环遍历包含所有所需数据的类,但代码返回第一个游戏的数据 9 次(有 9 个固定装置)。我做错了什么?
import scrapy
class SportsBetSpider(scrapy.Spider):
name = "odds"
def start_requests(self):
urls = [
"https://www.sportsbet.com.au/betting/australian-rules/afl/round-3"
]
for url in urls:
yield scrapy.Request(url=url,callback=self.parse)
def parse(self, response):
for post in response.css('li.cardOuterItem_fn8ai8t'):
yield{
'Team 1' : post.xpath('//span[@class="size12_fq5j3k2 normal_fgzdi7m caption_f4zed5e"]/text()')[0].get(),
'Odds 1' : post.xpath('//span[@class="size14_f7opyze medium_f1wf24vo priceTextSize_frw9zm9"]/text()')[0].get(),
'Team 2' : post.xpath('//span[@class="size12_fq5j3k2 normal_fgzdi7m caption_f4zed5e"]/text()')[1].get(),
'Odds 2' : post.xpath('//span[@class="size14_f7opyze medium_f1wf24vo priceTextSize_frw9zm9"]/text()')[1].get()
}
输出是:
2020-06-14 20:41:38 [scrapy.core.engine] 调试:已爬网 (200) https://www.sportsbet.com.au/betting/australian-rules/afl/round-3> (参考:无) 2020-06-14 20:41:38 [scrapy.core.scraper] 调试:从 https://www.sportsbet.com.au/betting/australian-rules/afl/round-3> 抓取 {'Team 1':'Richmond','赔率 1':'1.36','Team 2':'Hawthorn','赔率 2':'3.08'} 2020-06-14 20:41:38 [scrapy.core.scraper] 调试:从 https://www.sportsbet.com.au/betting/australian-rules/afl/round-3> 抓取 {'Team 1':'Richmond','赔率 1':'1.36','Team 2':'Hawthorn','赔率 2':'3.08'} 2020-06-14 20:41:38 [scrapy.core.scraper] 调试:从 https://www.sportsbet.com.au/betting/australian-rules/afl/round-3> 抓取 {'Team 1':'Richmond','赔率 1':'1.36','Team 2':'Hawthorn','赔率 2':'3.08'} 2020-06-14 20:41:38 [scrapy.core.scraper] 调试:从 https://www.sportsbet.com.au/betting/australian-rules/afl/round-3> 抓取 {'Team 1':'Richmond','赔率 1':'1.36','Team 2':'Hawthorn','赔率 2':'3.08'} 2020-06-14 20:41:38 [scrapy.core.scraper] 调试:从 https://www.sportsbet.com.au/betting/australian-rules/afl/round-3> 抓取 {'Team 1':'Richmond','赔率 1':'1.36','Team 2':'Hawthorn','赔率 2':'3.08'} 2020-06-14 20:41:38 [scrapy.core.scraper] 调试:从 https://www.sportsbet.com.au/betting/australian-rules/afl/round-3> 抓取 {'Team 1':'Richmond','赔率 1':'1.36','Team 2':'Hawthorn','赔率 2':'3.08'} 2020-06-14 20:41:38 [scrapy.core.scraper] 调试:从 https://www.sportsbet.com.au/betting/australian-rules/afl/round-3> 抓取 {'Team 1':'Richmond','赔率 1':'1.36','Team 2':'Hawthorn','赔率 2':'3.08'} 2020-06-14 20:41:38 [scrapy.core.scraper] 调试:从 https://www.sportsbet.com.au/betting/australian-rules/afl/round-3> 抓取 {'Team 1':'Richmond','赔率 1':'1.36','Team 2':'Hawthorn','赔率 2':'3.08'} 2020-06-14 20:41:38 [scrapy.core.scraper] 调试:从 {'Team 1':'Richmond','赔率 1':'1.36','Team 2':'Hawthorn','赔率 2':'3.08'} 2020-06-14 20:41:38 [scrapy.core.engine] 信息:关闭蜘蛛(完成)
只有9个其他灯具的时候才显示第一个灯具的抓取数据,这是怎么解决的?
【问题讨论】:
-
这个问题似乎很大程度上取决于您要抓取的页面的内容。也许您想创建一个示例,说明您正在抓取的页面的 HTML 是什么样的,并将其添加到您的问题中,以便人们可以更好地帮助您。
标签: parsing scrapy css-selectors web-crawler