I'm trying to scrape this site: http://stats.swehockey.se/ScheduleAndResults/Schedule/3940
And I've gotten as far (thanks to alecxe) as retrieving the date and teams.
from scrapy.item import Item, Field
from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
class SchemaItem(Item):
date = Field()
teams = Field()
class SchemaSpider(BaseSpider):
name = "schema"
allowed_domains = ["http://stats.swehockey.se/"]
start_urls = [
"http://stats.swehockey.se/ScheduleAndResults/Schedule/3940"
]
def parse(self, response):
hxs = HtmlXPathSelector(response)
rows = hxs.select('//table[@class="tblContent"]/tr')
for row in rows:
item = SchemaItem()
item['date'] = row.select('.//td[2]/div/span/text()').extract()
item['teams'] = row.select('.//td[3]/text()').extract()
yield item
So, my next step is to filter out anything that ins't a home game of "AIK" or "Djurg?rdens IF". After that I'll need to reformat to an .ics file which I can add to Google Calender.
EDIT: So I've solved a few things but still has a lot to do. My code now looks like this..
# -*- coding: UTF-8 -*-
from scrapy.item import Item, Field
from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
class SchemaItem(Item):
date = Field()
teams = Field()
class SchemaSpider(BaseSpider):
name = "schema"
allowed_domains = ["http://stats.swehockey.se/"]
start_urls = [
"http://stats.swehockey.se/ScheduleAndResults/Schedule/3940"
]
def parse(self, response):
hxs = HtmlXPathSelector(response)
rows = hxs.select('//table[@class="tblContent"]/tr')
for row in rows:
item = SchemaItem()
item['date'] = row.select('.//td[2]/div/span/text()').extract()
item['teams'] = row.select('.//td[3]/text()').extract()
for string in item['teams']:
teams = string.split('-') #split it
home_team = teams[0]#.split(' ') #only the first name, e.g. just 'Djurg?rdens' out of 'Djurg?rdens IF'
away_team = teams[1]
#home_team[0] = home_team[0].replace(" ", "") #remove whitespace
#home_team = home_team[0]
if "AIK" in home_team:
for string in item['date']:
year = string[0:4]
month = string[5:7]
day = string[8:10]
hour = string[11:13]
minute = string[14:16]
print year, month, day, hour, minute, home_team, away_team
elif u"Djurg?rdens" in home_team:
for string in item['date']:
year = string[0:4]
month = string[5:7]
day = string[8:10]
hour = string[11:13]
minute = string[14:16]
print year, month, day, hour, minute, home_team, away_team
That code prints out the games of "AIK", "Djurg?rdens IF" and "Skellefte? AIK". So my problem here is obviously how to filter out "Skellefte? AIK" games and if there is any easy way to make this program better. Thoughts on this?
Best regards!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…