我已经编写了一个scraper,它应该遍历每个足球队的几个页面,基本上获得所有历史数据,最终将每个页面合并成一个漂亮的json文件,每个足球队有一个条目。你知道吗
因此,我最终为每个团队提供了如下内容:
{'clubName': [u'West Ham United'],
'matches': [{'date': [u'17/08/1974'],
'opponent': [u'Manchester City'],
'place': [u'A'],
'results': [u'0:4 '],
'round': [u'1. Round'],
'time': []},
{'date': [u'19/08/1974'],
'opponent': [u'Luton Town'],
'place': [u'H'],
'results': [u'2:0 '],
'round': [u'2. Round'],
'time': []},
{'date': [u'24/08/1974'],
'opponent': [u'Everton FC'],
'place': [u'H'],
'results': [u'2:3 '],
'round': [u'3. Round'],
'time': []},
基本功能是:
为了调试,我已经在每个函数之后生成了项。我应该有20件东西。如果我在1、2和3个函数之后生成item,结果只有20行,这是完美的,但是在第4个函数中,它变得混乱,我最终得到数千个item。每个俱乐部有多个项目等
我最终会得到这样的东西:
{"matches": [], "clubName": ["Arsenal FC"]},
{"matches": [], "clubName": ["Arsenal FC"]},
{"matches": [], "clubName": ["Arsenal FC"]},
{"matches": [], "clubName": ["Arsenal FC"]},
{"matches": [], "clubName": ["Arsenal FC"]},
{"matches": [], "clubName": ["Arsenal FC"]},
{"matches": [], "clubName": ["Arsenal FC"]},
{"matches": [], "clubName": ["Arsenal FC"]},
{"matches": [], "clubName": ["Arsenal FC"]},
有时,除了俱乐部名称外,一排30个基本上是空白的项目。你知道吗
我是python的新手,我整个上午都在看这个东西,但我不知道为什么它不起作用。你知道吗
这是我的密码:
import scrapy
from ..items import PremierleagueItem
class PremierleagueSpider(scrapy.Spider):
name = "premierleague"
allowed_domains = ["worldfootball.net"]
start_urls = [
"http://www.worldfootball.net/competition/eng-premier-league/"
]
# get teams in the match
def parse(self, response):
for sel in response.xpath('//div[@id="tabelle_0"]/div[@class="data"]/table[1]/tr'):
clubName = sel.xpath('.//td[3]/a/text()').extract()
if clubName:
item = PremierleagueItem()
item['clubName'] = clubName
clubHref = sel.xpath('.//td[2]/a/@href').extract_first()
clubUrl = response.urljoin(clubHref)
request = scrapy.Request(clubUrl,callback=self.parse_get_historic_results_link)
request.meta['item'] = item
yield request
def parse_get_historic_results_link(self,response):
item = response.meta['item']
href2 = response.xpath('//div[@class="navibox2"]/div[@class="data"]/ul[5]/li[2]/a[1]/@href').extract_first()
url2 = response.urljoin(href2)
request = scrapy.Request(url2,callback=self.parse_seasons)
request.meta['item'] = item
yield request
def parse_seasons(self,response):
item = response.meta['item']
for sel in response.xpath('(//table[@class="standard_tabelle"])[1]/tr/td[2]/a'):
href = sel.xpath('.//@href').extract_first()
url = response.urljoin(href)
request = scrapy.Request(url,callback=self.parse_results)
request.meta['item'] = item
yield request
def parse_results(self,response):
item = response.meta['item']
item['matches'] = []
for sel in response.xpath('(//table[@class="standard_tabelle"])[1]/tr'):
results = sel.xpath('.//td[7]/a/text()').extract()
if results:
matchDict = {
'round' : sel.xpath('.//td[1]/a/text()').extract(),
'date' : sel.xpath('.//td[2]/a/text()').extract(),
'time' : sel.xpath('.//td[3]/text()').extract(),
'place' : sel.xpath('.//td[4]/text()').extract(),
'opponent' : sel.xpath('.//td[6]/a/text()').extract(),
'results' : results
}
item['matches'].append(matchDict)
yield item
我在这里塞了什么?你知道吗
编辑
只是澄清一下,我们最终得到的理想格式是多维数组,例如(伪代码):
Team name Y {
premierLeagueMatches {
{'date': [...],
'opponent': [...],
'place': [...],
'results': [...],
'round': [...],
'time': [...]
}
otherMatches {
same as above
}
},
Team name X {
premierLeagueMatches {
{'date': [...],
'opponent': [...],
'place': [...],
'results': [...],
'round': [...],
'time': [...]
}
otherMatches {
same as above
}
}
在数组的顶层,只有俱乐部名称,每个名称都是唯一的。没有重复的球队名称x或y等,但目前唯一唯一的关键是在顶级的赛季日期。你知道吗
在带有bug代码的json的最终输出中,我可以搜索“clubName”:[“West Ham United”],得到75个结果,而不是1个。因此,尽管有大量的数据可以追溯到19世纪:)而不是目前的1670个(我猜这是赛季总数*英超球队的数量),我试图最终只有20个项目(每支球队一个)。你知道吗
xpath在parse\u结果中出错,这是一个获取所需数据的可运行示例:
输出的一个片段:
你需要做更多的工作来获得你想要的精确格式,但是无论你做什么,你都需要使用正确的XPath,你也应该知道你要回到1900年左右,所以会有很多输出可能更适合db。我还从每一页中抽出第一个表格,当有多个表格是联赛结果时,有一些页面只有F.A杯结果等和yput球队等。。。如果你想得到所有的数据,它会是这样的:
在第一个表格的下半部分也有一些杯赛成绩,所以如果你只是想要英超联赛:
相关问题 更多 >
编程相关推荐