-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeeria_test.py
164 lines (124 loc) · 4.22 KB
/
feeria_test.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
# -*- coding: utf-8 -*-
from grab import Grab
from urllib import urlencode
from pyquery import PyQuery as pq
import urllib2
import pprint
import re, itertools
import MySQLdb
id_op = 3
country_id_opetator = 6
country_box = 85
adult_par = 3
child_par = 0
operator_url = 'http://samo.feeria.ua/search_tour?'
countries = [33]
adults = [
{'adult':1, 'child': 0},
{'adult':2, 'child': 0},
{'adult':3, 'child': 0},
{'adult':2, 'child': 1}]
db = MySQLdb.connect("localhost", "root", "alex", "tourbox_com_ua", charset='utf8')
dataPattern = re.compile(r'(\d{1,2}).(\d{1,2}).(\d{4})')
mealPattern = re.compile(r'^(\w{1,3})')
peoplePattern = re.compile(r'^(\d{1})')
def loadPage(url, adult, child, country, i):
print 'run Grab'
g = Grab()
g.setup(log_dir='feeria')
g.setup(timeout=250, connect_timeout=200)
g.setup(proxy='220.101.93.3:3128', proxy_type='http')
qs = urlencode({'samo_action':'PRICES',
'TOWNFROMINC':'2',
'STATEINC':country,
'TOURINC':'0',
'CHECKIN_BEG':'20160801',
'NIGHTS_FROM':'3',
'CHECKIN_END':'20160831',
'NIGHTS_TILL':'14',
'ADULT':adult,
'CURRENCY':'1',
'CHILD':child,
'TOWNTO_ANY':'1',
'TOWNTO':'',
'STARS_ANY':'1',
'STARS':'',
'hotelsearch':'0',
'HOTELS_ANY':'1',
'HOTELS':'',
'MEAL':'',
'FREIGHT':'0',
'FILTER':'0',
'HOTELTYPES':'',
'PACKET':'0',
'PRICEPAGE':i})
print (url + qs)
g.go(url + qs)
body = g.response.body
response = body[body.find('<table'):len(body)]
return response
def clear_string(str):
data_from_file = re.sub(r'\\n\\', '', str)
clear_data = re.sub(r' ', '', data_from_file)
clear_data = re.sub(r'\\', '', clear_data)
clear_data = re.sub(r'"', '', clear_data)
clear_data = re.sub(r' UAH', '', clear_data)
return clear_data
def insert_db(tour_name, nights, city, hotel, pitanie, room, adults, child, price, date, country, id):
with db:
cursor = db.cursor()
sql = '''INSERT INTO tours_parse(tur, nights, city, hotel, pitanie, room, adults, children, price, departure_date, country, tourop_id) \
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)'''
cursor.execute(sql, (tour_name, nights, city, hotel, pitanie, room, adults, child, price, date, country, id))
def parss(page_send):
print 'run parser'
page = page_send
new_page = page
sss = pq(new_page).find('tr:gt(0)')
for tr in sss('tr').items():
child = clear_string(tr.attr('data-child'))
adult = clear_string(tr.attr('data-adult'))
data = clear_string(tr('td:eq(0)').text())
search_result = dataPattern.search(data.encode('utf8')).groups()
date = '-'.join(reversed(search_result))
tur = tr('td:eq(1)').text().encode('utf8')
nights = tr('td:eq(2)').text()
hotel = clear_string(tr('td:eq(3)').text()).encode('utf8')
meal_from_file = clear_string(tr('td:eq(4)').text())
meal = mealPattern.search(meal_from_file).group().encode('utf8')
room = clear_string(tr('td:eq(5)').text()).encode('utf8')
price = clear_string(tr('td:eq(9)').text())
insert_db(tur, nights, '', hotel, meal, room, adult, child, price, date, country_box, id_op)
print tur
# print date
# print hotel
# print meal
# print room
# print adult
# print child
# print price
def parametrs_generator():
print 'run parametrs'
new_list = []
for country in countries:
for peoples in adults:
params = {'adult':peoples['adult'],'child': peoples['child'], 'country':country,}
new_list.append(params)
return new_list
def generator_url():
print 'run generator url'
parametrs_list = parametrs_generator()
i=1
k = -1
str = u'Данных не найдено. Измените параметры поиска'
ss = []
while k == -1:
print adult_par, child_par, country_id_opetator, i
stran = loadPage('http://samo.feeria.ua/search_tour?', adult_par, child_par, country_id_opetator, i)
ss.append(stran)
new_sring = stran.decode('cp1251')
k = new_sring.find(str)
parss(new_sring)
i = i + 1
generator_url()
db.close()