/
adv_crawler.py
153 lines (113 loc) · 4.21 KB
/
adv_crawler.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
import csv
import re
import urllib2
import mechanize
from bs4 import BeautifulSoup
import time
from twill import get_browser
import twill.commands as tw
STANDART_PAGE = 'https://www.erstenachhilfe.de/nachhilfelehrer?page={}'
PAGE_NUMBER = 1
LOGIN = 'arjeeee'
PASSWORD = 'RapidGrowth2015'
HOST_BLANK = 'https://www.erstenachhilfe.de{}'
class Crowler(object):
def __init__(self):
self.paginator = PAGE_NUMBER
self.browser = self.__create_session()
self.first_time = True
def __create_session(self):
browser = get_browser()
browser.go('http://www.erstenachhilfe.de/user?destination=node%2F767')
tw.fv('2', 'edit-name', LOGIN)
tw.fv('2', 'edit-pass', PASSWORD)
tw.showforms()
tw.submit('op')
return browser
def to_craw(self):
adverts = self.__find_adverts()
while adverts:
try:
advert_anchors = self.__find_links(adverts)
self.__collect_detail(advert_anchors)
except (mechanize._mechanize.BrowserStateError):
pass
except AttributeError:
adverts = False
else:
self.paginator += 1
adverts = self.__find_adverts()
self.__save_data()
def __find_adverts(self):
self.browser.go(STANDART_PAGE.format(str(self.paginator)))
html = self.browser.get_html()
soap = BeautifulSoup(html)
return soap.find_all('span',{'class': 'headline'})
def __find_links(self, content):
advert_anchors = []
for advert in content:
anchors = advert.find_all('a')
advert_anchors.append(anchors[0])
return advert_anchors
def __collect_detail(self, stack):
result = []
for anchor in stack:
link = HOST_BLANK.format(anchor.get('href'))
result.append(self.__parse_detail(link))
time.sleep(5)
self.__save_data(result)
def __parse_detail(self, detail_link):
result = {}
self.browser.go(detail_link)
html = self.browser.get_html()
soap = BeautifulSoup(html)
location = self.__get_location(soap)
result['link'] = detail_link.encode('utf-8')
result['subjects'] = self.__get_subjects(soap).encode('utf-8')
result['phone'] = self.__get_phone(soap).encode('utf-8')
result['city'] = location[1].encode('utf-8')
result['name'] = self.__get_name(soap).encode('utf-8')
result['zip_code'] = location[0]
result['url_picture'] = self.__get_picture_url(soap).encode('utf-8')
print result
return result
def __get_picture_url(self, soap):
tag = soap.find('img', {'id': 'avatar_img'})
url = tag.get('src')
if 'pictures' in url:
return url
return 'N/A'
def __get_name(self, soap):
tag = soap.find('div', {'class': 'kopf'})
text = tag.get_text().split(': ')
return text[1]
def __get_subjects(self, soap):
result = soap.find('td', {'class': 'bold'})
return result.get_text()
def __get_location(self, soap):
tags = soap.find('div', {'id': 'first_block'})
part_text = tags.get_text().split("PLZ Ort:")
result = part_text[1].split(' ')
""" Zip code will be result[0] """
return result
def __get_phone_from_ajax(self, link):
self.browser.go(link)
html = self.browser.get_html()
return html.split('</h3>')[1]
def __get_phone(self, soap):
anchor = soap.find('a', {'class': 'ajax_tip'})
if anchor:
link = HOST_BLANK.format(anchor.get('href'))
return self.__get_phone_from_ajax(link)
return 'N/A'
def __save_data(self, data):
with open('adverts.csv', 'a+') as f:
fieldnames = ['link', 'name', 'zip_code', 'subjects', 'city', 'phone', 'url_picture']
writer = csv.DictWriter(f, fieldnames=fieldnames)
if self.first_time:
writer.writeheader()
self.first_time = False
for row in data:
writer.writerow(row)
a = Crowler()
a.to_craw()