Created
February 7, 2014 00:29
-
-
Save lwiecek/8855318 to your computer and use it in GitHub Desktop.
Livescore results
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from lxml.html.soupparser import fromstring | |
import csv | |
r = requests.get('http://livescore.com') | |
root = fromstring(r.text) | |
home_teams = root.xpath('//td[@class="fh"]/text()') | |
away_teams = root.xpath('//td[@class="fa"]/text()') | |
scores = root.xpath('//td[@class="fs"]/a/text()') | |
with open('scores.csv', 'wb') as csvfile: | |
writer = csv.writer(csvfile) | |
for home, away, score in zip(home_teams, away_teams, scores): | |
try: | |
home_score, away_score = score.split('-') | |
row = [home.strip(), away.strip(), home_score.strip(), away_score.strip()] | |
print 'writing', row | |
writer.writerow(row) | |
except: # TODO failing due to some unicode stuff probably getting only ascii chars would fix that | |
pass # ignore for now |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment