-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawler_1.py
executable file
·175 lines (147 loc) · 5.92 KB
/
crawler_1.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import httplib
import sys
import re
import urllib
import csv
from HTMLParser import HTMLParser
class miniHTMLParser(HTMLParser):
viewedQueue = []
instQueue = []
tutorQueue = []
linkNumber = 0
def get_next_link(self):
if self.instQueue == []:
return ''
else:
return self.instQueue.pop(0)
def gethtmlfile(self, site, page):
try:
httpconn = httplib.HTTPConnection(site)
httpconn.request("GET", page)
resp = httpconn.getresponse()
resppage = resp.read()
except:
resppage = ""
return resppage
def handle_starttag(self, tag, attrs):
if tag == 'a':
newstr = str(attrs[0][1])
if re.search('http', newstr) == None:
if re.match('//', newstr) == None:
if re.search('mailto', newstr) == None:
# if ((newstr in self.tutorQueue) == False) and (re.search('matematik-', newstr) != None):
if ((newstr in self.tutorQueue) == False) and (newstr[1:2].isdigit() == True):
self.tutorQueue.append(newstr)
self.viewedQueue.append(newstr)
print " adding tutor count: ", str(len(self.tutorQueue)), newstr
self.parseTutor(newstr)
elif (newstr in self.viewedQueue) == False:
self.instQueue.append(newstr)
self.viewedQueue.append(newstr)
else:
print " ignoring", newstr
else:
print " ignoring", newstr
else:
print " ignoring", newstr
def parseTutor(self, url):
print "*******************************************************************************"
#analis
name = '-'
email = '-'
phone = '-'
city = '-'
regions = '-'
country = '-'
subject = '-'
urlPhoto = '-'
tut = "http://" + sys.argv[1] + url
self.linkNumber = self.linkNumber + 1
print str(self.linkNumber) + " url: " + tut
infile = urllib.urlopen(tut)
lines = infile.readlines()
for i in range(len(lines)):
line = lines[i]
if 'Ad/Soyad' in line:
without_space = lines[i+3].strip()
name = without_space[0:-5].strip()
print " name : " + name
if 'E-mail' in line:
without_space = lines[i+3].strip()
email = without_space[21:-20].strip()
print " email : " + email
if 'Telefon' in line:
without_space = lines[i+3].strip()
phone_dirty = without_space[0:-5].strip()
phone = ''
for c in phone_dirty:
if c in ('0','1','2','3','4','5','6','7','8','9'):
phone = phone + c
fivePos = phone.find('5')
if fivePos >= 0:
phone = phone[fivePos:fivePos + 10]
if len(phone) != 10:
phone = '-'
# if phone[0] == '0':
# phone = phone[1:]
# if phone[0] == '5' and len(phone) >= 10:
# phone = phone[:10]
# else:
# phone = '-'
print " phone : " + phone
if 'Ders verilebilece' in line:
without_space = lines[i+3].strip()
location = without_space[0:-5].strip()
pos = location.rfind(",")
if pos >= 0:
city = location[pos+1:].capitalize()
regions = location[0:pos].lower()
print " city : " + city
print " regions : " + regions
country = "Turkey"
print " country : " + country
subject = "Mathematics"
print " subject : " + subject
if 'retmen bilgiler' in line:
without_space = lines[i+6].strip()
urlPhoto = without_space[10:-5].strip()
urlPhoto = urlPhoto[0:-2].strip()
if 'nophoto' in urlPhoto:
urlPhoto = '-'
print " urlPhoto: " + urlPhoto
#Write to file
print "*******************************************************************************"
writer.writerow({'full-name': name, 'email': email, 'phone-number': phone, 'city': city, 'regions': regions, 'country': country, 'subject': subject, 'url-of-profile': tut, 'url-of-photo': urlPhoto})
def main():
if sys.argv[1] == '':
print "usage is ./minispider.py site link"
sys.exit(2)
mySpider = miniHTMLParser()
link = "/"
exceptionCount = 0
global csvfile
csvfile = open('tutors.csv', 'w')
fieldnames = ['full-name', 'email', 'phone-number', 'city', 'regions', 'country', 'subject', 'url-of-profile', 'url-of-photo']
global writer
writer = csv.DictWriter(csvfile, fieldnames = fieldnames)
writer.writeheader()
while link != '':
# if len(mySpider.tutorQueue) >= 200:
# print "count ref = " + str(len(mySpider.tutorQueue))
# break
print "Checking link out ", link
try:
retfile = mySpider.gethtmlfile(sys.argv[1], link)
mySpider.feed(retfile)
except UnicodeDecodeError:
exceptionCount = exceptionCount + 1
print "!!!!!!!!!!!!!!!!!!!!!! exception in link " + str(exceptionCount) + " " + link
link = mySpider.get_next_link()
csvfile.close()
mySpider.close()
print "\ndone\n"
print "exceptionCount: " + str(exceptionCount)
if __name__ == "__main__":
main()