-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcian_parse.py
161 lines (133 loc) · 6.82 KB
/
cian_parse.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
import requests
from lxml import html, etree
import config
from bs4 import BeautifulSoup
from controler import workerWithObject, workerWithRequest
import re
import telepot
import time
TOKEN = config.token # get token from command-line
bot = telepot.Bot(TOKEN)
def send_msg(text, abonents):
if len(str(abonents)) == 8 :
bot.sendMessage(abonents, text)
else:
for i in abonents:
bot.sendMessage(i, text)
def parsin_uri_and_insert(uri, count):
status = workerWithRequest.check_uri(uri)
print(status)
if status == False:
workerWithRequest.insert(uri)
send_msg('Добавленна новая ссылка для отслеживания', config.abonent)
status = workerWithRequest.check_uri(uri)
if status.count_objects != count.split(' ')[0] or status.count_objects == None:
send_msg('Колличество объектов изменено! Теперь в отслеживаемой ссылке {} объектов'.format(count.split(' ')[0]), config.abonent)
workerWithRequest.update(status.id, count.split(' ')[0])
return status.id
def parsing_object_and_insert(id, object):
for i in object:
if object[i] != {}:
obj = workerWithObject.check_object(object[i]['link'])
if obj == False:
workerWithObject.insert_object(id,
object[i]['link'],
object[i]['cost'],
object[i]['meter'],
object[i]['floor'],
None)
send_msg('Добавлен новый обьект!!!', config.abonent)
send_msg('Обьект: '
'Ссылка: {}\n'
'Цена: {}\n'
'м²: {}\n'
'Этаж:{}\n'.format(object[i]['link'],
object[i]['cost'],
object[i]['meter'],
object[i]['floor'],), config.abonent)
obj = workerWithObject.check_object(object[i]['link'])
if workerWithObject.diff_object(object[i]['link'],
object[i]['cost'],
object[i]['meter'],
object[i]['floor'],
None) == False:
workerWithObject.update_object(object[i]['link'],
object[i]['cost'],
object[i]['meter'],
object[i]['floor'],
object[i]['photo'],
obj.id)
send_msg('Объект изменился!', config.abonent)
send_msg('Обьект: '
'Ссылка: {}\n'
'Цена: {}\n'
'м²: {}\n'
'Этаж:{}\n'.format(object[i]['link'],
object[i]['cost'],
object[i]['meter'],
object[i]['floor'], ), config.abonent)
class cian_search:
def __init__(self, url):
self.r = requests.get(url)
if self.r.status_code != 200:
exit(1)
self.tree = html.fromstring(self.r.text)
def count_object(self):
for elem in self.tree.xpath('//*[@id="frontend-serp"]/div/div[3]/div[1]'):
a = etree.tostring(elem, pretty_print=True)
a = BeautifulSoup(a, "html.parser")
return str(a).split('>')[1][:-5]
def objects(self):
json = {}
for i in range(40):
json[i] = {}
for elem in self.tree.xpath('//*[@id="frontend-serp"]/div/div[4]/div[{}]/div/div[2]/div[2]/a'.format(i)):
a = etree.tostring(elem, pretty_print=True)
a = BeautifulSoup(a, "html.parser")
link = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', str(a))[0]
#print(link)
json[i]['link'] = link
for elem in self.tree.xpath('//*[@id="frontend-serp"]/div/div[4]/div[{}]/'
'div/div[2]/div[1]/div[1]/div[1]/div[2]/div/div[1]'.format(i)):
a = etree.tostring(elem, pretty_print=True)
a = BeautifulSoup(a, "html.parser")
cost = re.findall('>.*<', str(a))[0][1:-1]
json[i]['cost'] = cost.split(' ')[0]
for elem in self.tree.xpath('//*[@id="frontend-serp"]/div/div[4]/div[{}]/div/'
'div[2]/div[1]/div[1]/div[1]/div[4]/div/div[1]'.format(i)):
a = etree.tostring(elem, pretty_print=True)
a = BeautifulSoup(a, "html.parser")
meter = re.findall('>.*<', str(a))[0][1:-1]
json[i]['meter'] = meter.split(' ')[0]
#
for elem in self.tree.xpath(
'//*[@id="frontend-serp"]/div/div[4]/div[{}]/div/'
'div[2]/div[1]/div[1]/div[1]/div[5]/div/div[1]'.format(
i)):
a = etree.tostring(elem, pretty_print=True)
a = BeautifulSoup(a, "html.parser")
floor = re.findall('>.*<', str(a))[0][1:-1]
json[i]['floor'] = floor.split(' ')[0]
#
for elem in self.tree.xpath(
'//*[@id="frontend-serp"]/div/div[4]/div[{}]/div/div[1]/div/div[1]/div/div/div/img'.format(
i)):
a = etree.tostring(elem, pretty_print=True)
a = BeautifulSoup(a, "html.parser")
photoUrl = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', str(a))[0]
json[i]['photo'] = photoUrl
return json
def page(self, id):
for elem in self.tree.xpath('//*[@id="frontend-serp"]/div/div[6]/div/ul'):
a = etree.tostring(elem, pretty_print=True)
a = BeautifulSoup(a, "html.parser")
link = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', str(a))
for i in link:
init = cian_search(i)
parsing_object_and_insert(id, init.objects())
while True:
init = cian_search(config.url)
id = parsin_uri_and_insert(config.url, init.count_object())
parsing_object_and_insert(id, init.objects())
init.page(id)
time.sleep(1800)