-
Notifications
You must be signed in to change notification settings - Fork 7
/
avito_parsing.py
582 lines (490 loc) · 24.3 KB
/
avito_parsing.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup
import time
import random
import datetime
from fake_useragent import UserAgent
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from xvfbwrapper import Xvfb
from PIL import Image
from pytesseract import image_to_string
import sys
from database import DataBase
# на каких записях останавливаться
with open("breakpoints/avito.txt", "r", encoding="utf8") as file:
breakpoints = file.readlines()
try:
break_apartment = tuple(breakpoints[0].strip().split("--"))
except:
break_apartment = None
try:
break_cottage = tuple(breakpoints[1].strip().split("--"))
except:
break_cottage = None
try:
break_land = tuple(breakpoints[2].strip().split("--"))
except:
break_land = None
try:
break_commercial = tuple(breakpoints[3].strip().split("--"))
except:
break_commercial = None
#defining chrome options for selenium
options = Options()
options.add_argument('--no-sandbox')
db = DataBase()
visited_urls = []
def get_html(url):
req = requests.get(url, headers={"User-Agent": UserAgent().chrome})
return req.text.encode(req.encoding)
def get_total_pages(html):
soup = BeautifulSoup(html, "lxml")
try:
pages = soup.find("div", class_="pagination-pages clearfix").find_all("a", class_="pagination-page")[-1].get("href")
total_pages = int(pages.split("=")[1].split("&")[0])
except Exception as e:
with open("logs.txt", "a", encoding="utf8") as file:
file.write(str(e) + " avito get_total_pages\n")
sys.exit(0)
return total_pages
def get_title(soup):
try:
title = soup.find("span", class_="title-info-title-text").text.strip()
except Exception as e:
with open("logs.txt", "a", encoding="utf8") as file:
file.write(str(e) + " avito get_title\n")
title = "Не указано"
return title
def get_address(soup):
try:
address = "{}, {}".format(soup.find("meta", itemprop="addressLocality").get("content").strip(),
soup.find("span", itemprop="streetAddress").text.strip())
# separating data from the address string
district, street = "Не указано", "Не указано"
city = address.split(",")[0]
block_number = address.split(",")[-1].strip()
if "ул " in block_number.lower() or "ул." in block_number.lower() or "улица" in block_number.lower()\
or " пер" in block_number.lower() or "проспект" in block_number.lower() or "проезд" in block_number.lower():
street = block_number
block_number = "Не указано"
for param in address.split(",")[1:-1]:
if "ул " in param.lower() or "ул." in param.lower() or "улица" in param.lower() \
or " пер" in param.lower() or "проспект" in param.lower() or "проезд" in param.lower():
street = param.strip()
elif "район" in param.lower() or "р-н" in param.lower():
district = param.strip()
if street.split()[-1].strip().isdigit():
block_number = street.split()[-1].strip()
street = " ".join(street.split()[:-1]).strip()
return city, district, street, block_number
except Exception as e:
with open("logs.txt", "a", encoding="utf8") as file:
file.write(str(e) + " avito get_address\n")
return ["Не указано"] * 4
def get_selling_info(soup):
try:
per_meter = False # если цена указана за квадратный метр
price = soup.find("span", class_="price-value-string js-price-value-string").text.strip()
if "за сутки" in price:
sell_type = "Аренда"
rent_info = "посуточно"
elif "в месяц" in price:
sell_type = "Аренда"
rent_info = "длительный срок"
if "за " in price:
per_meter = True
else:
sell_type = "Продажа"
rent_info = "Не аренда"
price = soup.find("span", class_="js-item-price").text.strip()
# ошибка кодировки при записи, собираем сообщение вручную
if rent_info == "посуточно":
price = "от " + price + " за сутки"
elif rent_info == "длительный срок":
if per_meter:
price = price + " в месяц за м2"
else:
price = price + " в месяц"
except Exception as e:
with open("logs.txt", "a", encoding="utf8") as file:
file.write(str(e) + " avito get_selling info\n")
sell_type, price, rent_info = ["Не указано"] * 3
return sell_type, price, rent_info
def get_deposit(soup):
try:
deposit = soup.find("div", class_="item-price-sub-price").text.strip()
except Exception as e:
with open("logs.txt", "a", encoding="utf8") as file:
file.write(str(e) + " avito get_deposit\n")
deposit = "Не указано"
return deposit
def get_seller_type(soup):
try:
seller_type = soup.find("div", class_="seller-info-prop seller-info-prop_short_margin")
if seller_type is not None:
seller_type = "Посредник"
else:
seller_type = "Собственник"
except Exception as e:
with open("logs.txt", "a", encoding="utf8") as file:
file.write(str(e) + " avito get_seller_type\n")
seller_type = "Не указано"
return seller_type
def get_seller_name(soup):
try:
seller_name = soup.find("div", class_="seller-info-name").find("a").text.strip()
except Exception as e:
with open("logs.txt", "a", encoding="utf8") as file:
file.write(str(e) + " avito get_seller_name\n")
seller_name = "Не указано"
return seller_name
def get_photos(soup):
try:
images = []
images_list = soup.find("ul", class_="gallery-list js-gallery-list").find_all("li", class_="gallery-list-item js-gallery-list-item")
for image in images_list:
link = image.find("span").get("style").split(":")[1].strip()[4:-2]
images.append(link)
images = "\n".join(images)
except:
# если нет фото, возьмем фото с "обложки"
try:
images = soup.find("span", class_="gallery-img-cover").get("style").split(":")[1].strip()[4:-2]
except Exception as e:
with open("logs.txt", "a", encoding="utf8") as file:
file.write(str(e) + " avito get_photos\n")
images = "Не указано"
return images
def get_description(soup):
try:
description = soup.find("div", class_="item-description-text").find("p").text.strip()
except Exception as e:
with open("logs.txt", "a", encoding="utf8") as file:
file.write(str(e) + " avito get_description\n")
description = "Не указано"
return description
def get_date(soup):
try:
date = soup.find("div", class_="title-info-metadata-item").text.split(",")[1].strip()
if "сегодня" in date:
date = str(datetime.datetime.today()).split()[0]
elif "вчера" in date:
date = str(datetime.datetime.today() - datetime.timedelta(days=1)).split()[0]
else:
date = "too old"
except Exception as e:
with open("logs.txt", "a", encoding="utf8") as file:
file.write(str(e) + " avito get_date\n")
date = "Не указано"
return date
def get_seller_phone(url):
# телефон показывается в виде картинки, используем selenium и pytesseract
vdisplay = Xvfb()
vdisplay.start()
driver = webdriver.Chrome(options=options)
driver.set_window_size(1920, 1080)
driver.get(url)
try:
button = driver.find_element_by_xpath('//a[@class="button item-phone-button js-item-phone-button '
'button-origin button-origin-blue button-origin_full-width '
'button-origin_large-extra item-phone-button_hide-phone '
'item-phone-button_card js-item-phone-button_card"]')
button.click()
time.sleep(2)
driver.save_screenshot("phone_number.png")
image = driver.find_element_by_xpath('//div[@class="item-phone-big-number js-item-phone-big-number"]//*')
cropped = Image.open("phone_number.png")
x, y = image.location["x"], image.location["y"]
width, height = image.size["width"], image.size["height"]
cropped.crop((x, y, x + width, y + height)).save("phone.gif")
phone = Image.open("phone.gif")
phone_text = image_to_string(phone)
except Exception as e:
with open("logs.txt", "a", encoding="utf8") as file:
file.write(str(e) + " avito get_seller_phone\n")
phone_text = "Не указано"
driver.quit()
vdisplay.stop()
return phone_text
def get_apartment_params(soup):
rooms_number, floor_number, total_floors, material, total_area, kitchen_area, living_area = ["Не указано"] * 7
block_type = "Вторичка"
try:
params = soup.find_all("li", class_="item-params-list-item")
for i in range(len(params)):
info = params[i].text.strip()
if "Количество комнат" in info:
rooms_number = info.split(":")[1].strip()
elif "Этажей в доме" in info:
total_floors = info.split(":")[1].strip()
elif "Этаж" in info:
floor_number = info.split(":")[1].strip()
elif "Тип дома" in info:
material = info.split(":")[1].strip()
elif "Общая площадь" in info:
total_area = info.split(":")[1].split("м²")[0].strip()
elif "Площадь кухни" in info:
kitchen_area = info.split(":")[1].split("м²")[0].strip()
elif "Жилая площадь" in info:
living_area = info.split(":")[1].split("м²")[0].strip()
elif "Официальный застройщик" in info or "Название объекта недвижимости" in info:
block_type = "Новостройка"
except Exception as e:
with open("logs.txt", "a", encoding="utf8") as file:
file.write(str(e) + " avito get_apartment_params\n")
return rooms_number, floor_number, total_floors, material, total_area, kitchen_area, living_area, block_type
def get_cottage_params(soup):
house_type, total_floors, distance, material, total_area, land_area = ["Не указано"] * 6
try:
params = soup.find_all("li", class_="item-params-list-item")
for i in range(len(params)):
info = params[i].text.strip()
if "Вид объекта" in info:
house_type = info.split(":")[1].strip()
elif "Этажей в доме" in info:
total_floors = info.split(":")[1].strip()
elif "Расстояние до города" in info:
distance = info.split(":")[1].split("км")[0].strip() + " км"
elif "Материал стен" in info:
material = info.split(":")[1].strip()
elif "Площадь дома" in info:
total_area = info.split(":")[1].split("м²")[0].strip()
elif "Площадь участка" in info:
land_area = info.split(":")[1].split("сот")[0].strip() + " сот"
except Exception as e:
with open("logs.txt", "a", encoding="utf8") as file:
file.write(str(e) + " avito get_cottage_params\n")
return house_type, total_floors, distance, material, total_area, land_area
def get_land_params(soup):
distance, area = "Не указано", "Не указано"
try:
labels = soup.find_all("span", class_="item-params-label")
params = soup.find("div", class_="item-params").find_all("span")
for i in range(len(labels)):
info = params[i * 2].text.strip()
label = labels[i].text.strip()
if "Расстояние до города" in label:
distance = info.split(":")[1].split("км")[0].strip() + " км"
elif "Площадь" in label:
area = info.split(":")[1].split("сот")[0].strip() + " сот"
except Exception as e:
with open("logs.txt", "a", encoding="utf8") as file:
file.write(str(e) + " avito get_land_params\n")
return distance, area
def get_commercial_params(soup):
office_class, area = "Не указано", "Не указано"
try:
labels = soup.find_all("span", class_="item-params-label")
params = soup.find("div", class_="item-params").find_all("span")
for i in range(len(labels)):
info = params[i * 2].text.strip()
label = labels[i].text.strip()
if "Площадь" in label:
area = info.split(":")[1].split("м²")[0].strip()
elif "Класс здания" in label:
office_class = info.split(":")[1].strip()
except Exception as e:
with open("logs.txt", "a", encoding="utf8") as file:
file.write(str(e) + " avito get_commercial_params\n")
return office_class, area
def get_apartment_data(url, html):
soup = BeautifulSoup(html, "lxml")
title = get_title(soup)
if "сниму" not in title.lower() and "куплю" not in title.lower():
city, district, street, block_number = get_address(soup)
sell_type, price, rent_info = get_selling_info(soup)
rooms_number, floor_number, total_floors, material, total_area, kitchen_area, living_area, block_type = get_apartment_params(soup)
#seller_type = get_seller_type(soup)
#seller_name = get_seller_name(soup)
images = get_photos(soup)
description = get_description(soup)
phone = get_seller_phone(url)
date = get_date(soup)
selling_detail = "Не указано" # на авито не указывается эта информация
return [city, district, street, block_number, sell_type, rent_info, price, block_type,
rooms_number, total_area, total_floors, material, selling_detail, images,
description, date, phone, kitchen_area, living_area, floor_number]
return None
def get_cottage_data(url, html):
soup = BeautifulSoup(html, "lxml")
title = get_title(soup)
if "сниму" not in title.lower() and "куплю" not in title.lower():
city, district, street, block_number = get_address(soup)
sell_type, price, rent_info = get_selling_info(soup)
house_type, total_floors, distance, material, total_area, land_area = get_cottage_params(soup)
#seller_type = get_seller_type(soup)
seller_name = get_seller_name(soup)
images = get_photos(soup)
description = get_description(soup)
phone = get_seller_phone(url)
date = get_date(soup)
selling_detail, comforts, land_status = ["Не указано"] * 3 # на авито не указывается эта информация
return [city, district, street, block_number, sell_type, rent_info, price, house_type,
total_area, comforts, selling_detail, images, description, date, phone, material,
total_floors, land_area, land_status, seller_name]
return None
def get_land_data(url, html):
soup = BeautifulSoup(html, "lxml")
title = get_title(soup)
if "сниму" not in title.lower() and "куплю" not in title.lower():
# категория земель указывается в скобках в названии объявления
if "(" in title:
land_type = title[title.find("(") + 1:].split(")")[0]
else:
land_type = "Не указано"
city, district, street, _ = get_address(soup)
sell_type, price, _ = get_selling_info(soup)
if "Аренда" in sell_type:
deposit = get_deposit(soup)
else:
deposit = "Не аренда"
distance, area = get_land_params(soup)
seller_type = get_seller_type(soup)
seller_name = get_seller_name(soup)
images = get_photos(soup)
description = get_description(soup)
phone = get_seller_phone(url)
date = get_date(soup)
return [city, district, street, sell_type, deposit, land_type, distance, area, price, seller_type, images,
description, seller_name, phone, date]
return None
def get_commercial_data(url, html):
soup = BeautifulSoup(html, "lxml")
title = get_title(soup)
if "сниму" not in title.lower() and "куплю" not in title.lower():
# анализируем вид помещения по заголовку
if "офис" in title.lower():
object_type = "Офисное помещение"
elif "торг" in title.lower():
object_type = "Торговое помещение"
elif "гостиница" in title.lower():
object_type = "Гостиница"
elif "свобод" in title.lower():
object_type = "Помещение свободного назначения"
elif "производ" in title.lower():
object_type = "Производственное помещение"
elif "склад" in title.lower():
object_type = "Складское помещение"
else:
object_type = "Не указано"
city, district, street, block_number = get_address(soup)
sell_type, price, _ = get_selling_info(soup)
# if "Аренда" in sell_type:
# deposit = get_deposit(soup)
# else:
# deposit = "Не аренда"
# если не офис, не заполняем поле office_class
if object_type == "Офисное помещение":
office_class, area = get_commercial_params(soup)
else:
_, area = get_commercial_params(soup)
office_class = "Не офис"
#seller_type = get_seller_type(soup)
seller_name = get_seller_name(soup)
images = get_photos(soup)
description = get_description(soup)
phone = get_seller_phone(url)
date = get_date(soup)
furniture, entrance = "Не указано", "Не указано" # на авито не указывается эта информация
return [city, district, street, block_number, sell_type, price, object_type, office_class,
furniture, entrance, area, date, phone, images, description, seller_name]
return None
def crawl_page(first_offer, html, category):
global visited_urls, db
soup = BeautifulSoup(html, "lxml")
try:
offers = soup.find("div", class_="catalog-list").find_all("div", class_="item_table")
except:
offers = []
if offers is None or not offers:
print("Парсинг завершен avito")
return True
for offer in offers:
try:
if first_offer:
# сохраняем самую первую запись как точку выхода
modifier = "w" if category == "Квартиры" else "a"
with open("breakpoints/avito.txt", modifier, encoding="utf8") as file:
file.write("%s--%s\n" % (offer.find("a", class_="item-description-title-link").get("title"),
offer.find("span", {"class": "price", "itemprop": "price"}).get("content")))
first_offer = False
if offer.find("div", class_="js-item-date c-2").text.strip() == "2 дня назад":
print("Парсинг завершен avito")
return True
key_info = (offer.find("a", class_="item-description-title-link").get("title"), offer.find("span", {"class": "price", "itemprop": "price"}).get("content"))
if any(x == key_info for x in [break_apartment, break_cottage, break_land, break_commercial]):
print("Парсинг завершен avito")
return True
url = "https://avito.ru" + offer.find("div", class_="description").find("h3").find("a").get("href")
if url in visited_urls:
print("avito not unique")
time.sleep(random.uniform(5, 8))
continue
else:
visited_urls.append(url)
data = []
if category == "Квартиры":
data = get_apartment_data(url, get_html(url))
# записываем ключевую информацию, чтобы потом найти дубликаты
with open("total_data.txt", "a", encoding="utf8") as file:
file.write("%s--%s--%s--%s--%s--%s\n" % (data[2], data[3], data[4], data[8], data[-1], url))
elif category == "Дома":
data = get_cottage_data(url, get_html(url))
with open("total_data.txt", "a", encoding="utf8") as file:
file.write("%s--%s--%s--%s--%s\n" % (data[2], data[3], data[7], data[8], url))
elif category == "Участки":
data = get_land_data(url, get_html(url))
with open("total_data.txt", "a", encoding="utf8") as file:
file.write("%s--%s--%s--%s\n" % (data[2], data[5], data[7], url))
elif category == "Коммерческая_недвижимость":
data = get_commercial_data(url, get_html(url))
with open("total_data.txt", "a", encoding="utf8") as file:
file.write("%s--%s--%s--%s--%s\n" % (data[2], data[3], data[6], data[10], url))
if data[0] != "Не указано" and data is not None:
try:
db.insert_data(category, data)
except:
db.close()
db = DataBase()
db.insert_data(category, data)
print("parsed page avito")
#print(data)
except Exception as e:
with open("logs.txt", "a", encoding="utf8") as file:
file.write(str(e) + " avito crawl_page\n")
#print(str(e) + " avito crawl_page")
time.sleep(random.uniform(5, 8))
def parse(category_url, base_url, category_name):
page_part = "p="
parameters_part = "&s=104&s_trg=3&bt=1"
total_pages = get_total_pages(get_html(category_url))
for page in range(1, total_pages + 1):
url_gen = base_url + page_part + str(page) + parameters_part
if page == 1:
completed = crawl_page(True, get_html(url_gen), category_name)
else:
completed = crawl_page(False, get_html(url_gen), category_name)
if completed:
break
def main():
global visited_urls
url_apartments = "https://www.avito.ru/saratovskaya_oblast/kvartiry?p=1&s=104&s_trg=3&bt=1"
base_url = "https://www.avito.ru/saratovskaya_oblast/kvartiry?"
parse(url_apartments, base_url, "Квартиры")
visited_urls = []
url_cottages = "https://www.avito.ru/saratovskaya_oblast/doma_dachi_kottedzhi?s=104&s_trg=3&bt=1"
base_url = "https://www.avito.ru/saratovskaya_oblast/doma_dachi_kottedzhi?"
parse(url_cottages, base_url, "Дома")
visited_urls = []
url_lands = "https://www.avito.ru/saratovskaya_oblast/zemelnye_uchastki?s=104&s_trg=3&bt=1"
base_url = "https://www.avito.ru/saratovskaya_oblast/zemelnye_uchastki?"
parse(url_lands, base_url, "Участки")
visited_urls = []
url_commercials = "https://www.avito.ru/saratovskaya_oblast/kommercheskaya_nedvizhimost?s=104&s_trg=3&bt=1"
base_url = "https://www.avito.ru/saratovskaya_oblast/kommercheskaya_nedvizhimost?"
parse(url_commercials, base_url, "Коммерческая_недвижимость")
if __name__ == "__main__":
main()
db.close()