-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtenniscrawler.py
129 lines (127 loc) · 4.37 KB
/
tenniscrawler.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
def crawl(u):
try:
dic={}
url=str(u)
dic["Link"]=url
page=urllib2.urlopen(url).read()
d=page
d=page.split("<title>",1)
d=d[1]
d=d.split("</title>",1)
d=d[0]
d=d.split(" - ",1)
name=d[0].strip()
dic["Name"]=name
data=page.split('<table class="plDetail">',1)
data=data[1]
data=data.split("</table>",1)
data=data[0]
soup=bs(str(data))
data1=soup.find_all("td")
imgtd=str(data1[0])
imgtd=imgtd.split('src="',1)
imgtd=imgtd[1]
imgtd=imgtd.split('"',1)
image="http://www.tennisexplorer.com"+str(imgtd[0])
dic["Image"]=image
soup1=bs(str(data1))
div=soup1.findAll("div",class_="date")
for i in div:
a=i.text
a=a.split(":",1)
dic[a[0].title()]=a[1].title()
if dic.has_key("Current/Highest Rank - Singles"):
d1=str(dic["Current/Highest Rank - Singles"])
if "- / " in d1:
d1=d1.split("- / ")
dic.pop("Current/Highest Rank - Singles")
if len(d1[0].replace(".","").strip())!=0:
dic["Current Rank - Singles"]=str(d1[0]).replace(".","").strip()
if len(str(d1[1]).replace(".","").strip())!=0:
dic["Highest Rank - Singles"]=str(d1[1]).replace(".","").strip()
elif " / " in d1:
d1=d1.split(" / ")
dic.pop("Current/Highest Rank - Singles")
if len(d1[0].replace(".","").strip())!=0:
dic["Current Rank - Singles"]=str(d1[0]).replace(".","").strip()
if len(str(d1[1]).replace(".","").strip())!=0:
dic["Highest Rank - Singles"]=str(d1[1]).replace(".","").strip()
elif " - / " in d1:
d1=d1.split(" - / ")
dic.pop("Current/Highest Rank - Singles")
if len(d1[0].replace(".","").strip())!=0:
dic["Current Rank - Singles"]=str(d1[0]).replace(".","").strip()
if len(str(d1[1]).replace(".","").strip())!=0:
dic["Highest Rank - Singles"]=str(d1[1]).replace(".","").strip()
elif ". / " in d1:
d1=d1.split(". / ")
dic.pop("Current/Highest Rank - Singles")
if len(d1[0].replace(".","").strip())!=0:
dic["Current Rank - Singles"]=str(d1[0]).replace(".","").strip()
if len(str(d1[1]).replace(".","").strip())!=0:
dic["Highest Rank - Singles"]=str(d1[1]).replace(".","").strip()
else:
d1=d1.replace(".","").strip()
dic.pop("Current/Highest Rank - Singles")
if len(d1.replace(".","").strip())!=0:
dic["Highest Rank - Singles"]=str(d1).replace(".","").strip()
if dic.has_key("Current/Highest Rank - Doubles"):
df=str(dic["Current/Highest Rank - Doubles"])
if "- / " in df:
df=df.split("- / ")
dic.pop("Current/Highest Rank - Doubles")
if len(df[0].replace(".","").strip())!=0:
dic["Current Rank - Doubles"]=str(df[0]).replace(".","").strip()
if len(df[1].replace(".","").strip())!=0:
dic["Highest Rank - Doubles"]=str(df[1]).replace(".","").strip()
elif " / " in df:
df=df.split(" / ")
dic.pop("Current/Highest Rank - Doubles")
if len(df[0].replace(".","").strip())!=0:
dic["Current Rank - Doubles"]=str(df[0]).replace(".","").strip()
if len(df[1].replace(".","").strip())!=0:
dic["Highest Rank - Doubles"]=str(df[1]).replace(".","").strip()
elif " - / " in df:
df=df.split(" - / ")
dic.pop("Current/Highest Rank - Doubles")
if len(df[0].replace(".","").strip())!=0:
dic["Current Rank - Doubles"]=str(df[0]).replace(".","").strip()
if len(df[1].replace(".","").strip())!=0:
dic["Highest Rank - Doubles"]=str(df[1]).replace(".","").strip()
elif ". / " in df:
df=df.split(". / ")
dic.pop("Current/Highest Rank - Doubles")
if len(df[0].replace(".","").strip())!=0:
dic["Current Rank - Doubles"]=str(df[0]).replace(".","").strip()
if len(df[1].replace(".","").strip())!=0:
dic["Highest Rank - Doubles"]=str(df[1]).replace(".","").strip()
else:
df=df.replace(".","").strip()
dic.pop("Current/Highest Rank - Doubles")
if len(df.replace(".","").strip())!=0:
dic["Highest Rank - Doubles"]=str(df).replace(".","").strip()
tennis1.insert(dic)
except:
pass
import urllib2
import MySQLdb
import pymongo
import threading
T = threading.Thread
from bs4 import BeautifulSoup as bs
c = pymongo.Connection()
c['dbname'].drop_collection('tennis1')
client=pymongo.MongoClient("ip")
db=client.dbname
tennis1=db.tennis1
conn=MySQLdb.connect("localhost","user","pwd","dbname")
co=conn.cursor()
sql="SELECT `link` from `tennis`;"
co.execute(sql)
res=co.fetchall()
for r in res:
u=str(r[0])
while threading.active_count()>200:
continue
t = T(target=crawl,args=(u,))
t.start()