-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeautifulsoup.py
executable file
·149 lines (138 loc) · 4.48 KB
/
beautifulsoup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import urllib2;
import re;
import csv;
import Lineup;
from bs4 import BeautifulSoup
def existDate(dateList, newDate):
result = "true";
for i in range(len(dateList)):
#print "list: ", dateList[i];
#print "new: ", newDate;
if cmp(newDate, dateList[i]) == 0:
result = "true";
break;
else:
result = "false";
#print "result: ", result;
return result;
def extractDate(htmlTable):
dateLine = htmlTable.find_all("tr", "stathead");
return dateLine[0].string;
def extractHome(htmlTable):
home = [];
dateLine = htmlTable.find_all("td", align="right");
#print dateLine;
for a in dateLine:
homeLine = a.find_all("a");
if (homeLine):
home.append(homeLine[0].string);
#print homeLine[0].string;
return home;
def extractScore(htmlTable):
scores = [];
scoreLine = htmlTable.find_all("td", align="center");
#print scoreLine;
for a in scoreLine:
score = a.find_all("a");
if (score):
scores.append(score[0].string);
return scores;
def extractLinks(htmlTable):
links = [];
linksLine = htmlTable.find_all("td", align="center");
#print linksLine;
for a in linksLine:
link = a.find_all("a");
if (link):
links.append(link[0].get('href'));
return links;
def extractAway(htmlTable):
aways = [];
awaysLine = htmlTable.find_all("td", align="left");
#print awaysLine;
for a in awaysLine:
away = a.find_all("a");
if (away):
aways.append(away[0].string);
return aways;
def writeCSV_init():
fileWriter = csv.writer(open('score.csv', 'a'));
fileWriter.writerow(['Home', 'Score', 'Away']);
def writeCSV(bDate, bHome, bScore, bAway):
fileWriter = csv.writer(open('lineup.csv', 'ab'));
# for i in bHome:
# for a in bScore:
# for b in bAway:
# fileWriter.writerow([i] + [a] + [b]);
for i in range(len(bHome)):
fileWriter.writerow([bHome[i]] + [bScore[i]] + [bAway[i]]);
page = 20120815;
#writeCSV_init();
battleDate = [];
battleDate.append('Friday, August 17, 2012');
while page <= 20130519:
page = str(page);
print page;
try:
response = urllib2.urlopen('http://espnfc.com/results/_/date/'+page+'/league/eng.1/english-premier-league?cc=5901');
#response = urllib2.urlopen('http://espnfc.com/results/_/date/2012916/league/eng.1/english-premier-league?cc=5901');
html = response.read();
except urllib2.HTTPError, error:
html = error.read();
soup = BeautifulSoup(html);
battleHome = [];
battleScore = [];
battleLinks = [];
battleAway = [];
homePosition = [];
homeID = [];
homeName = [];
awayPosition = [];
awayID = [];
awayName = [];
#print soup.prettify();
#print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
for a in soup.find_all('table'):
#print a, "\n";
#print "Date: ", extractDate(a);
if existDate(battleDate, extractDate(a)) == "true":
print "same\n";
else:
battleDate.append(extractDate(a));
battleHome = extractHome(a);
#for i in range(len(battleHome)):
#print "Home: ", battleHome[i];
battleScore = extractScore(a);
#for i in range(len(battleScore)):
# print "Score: ", battleScore[i];
battleLinks = extractLinks(a);
#for i in range(len(battleLinks)):
# print "Link: ", battleLinks[i];
battleAway = extractAway(a);
#for i in range(len(battleAway)):
# print "Away: ", battleAway[i];
#writeCSV(battleDate, battleHome, battleScore, battleAway);
Lineup.main_1(battleLinks, battleHome, battleAway);
page = int(page);
page = page + 15;
# num = page % 100;
# numMonth = page % 10000;
# if num >= 40:
# page = page - 48 + 100;
# if numMonth >= 1300:
# page = page - 1308 + 10000;
# except urllib2.HTTPError, error:
# print "error\n", page;
# html = error.read();
# soup = BeautifulSoup(html);
# print(soup.prettify());
# page = int(page);
# page = page + 16;
# num = page % 100;
# numMonth = page % 10000;
# if num >= 40:
# page = page - 48 + 100;
# if numMonth >= 1300:
# page = page - 1308 + 10000;
# contents = error.read();
# continue;