forked from Minuta23321/Bot-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
373 lines (300 loc) · 11.9 KB
/
Copy pathparser.py
File metadata and controls
373 lines (300 loc) · 11.9 KB
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
import logging
import time
import asyncio
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
from bs4 import BeautifulSoup
from telegram import Bot
from selenium.common.exceptions import TimeoutException
import re
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s %(levelname)s: %(message)s'
)
# ============================================================
# TELEGRAM
# ============================================================
TELEGRAM_TOKEN = '8664437815:AAF5TjvMm7IxIvUpLCcS9XpUDwlM_wOw7Bs'
PRIMARY_CHAT_ID = '1834103343'
SECONDARY_CHAT_ID = '1858170014'
# ============================================================
# ФИЛЬТРЫ ЦЕН (бот отправит только если цена в диапазоне)
# ============================================================
PRICE_EUR_MIN = 150 # € минимум
PRICE_EUR_MAX = 300 # € максимум
PRICE_USD_MIN = 200 # $ минимум
PRICE_USD_MAX = 350 # $ максимум
PRICE_MDL_MIN = 3000 # лей минимум
PRICE_MDL_MAX = 6000 # лей максимум
# ============================================================
# ФИЛЬТРЫ 999.MD
# Районы Кишинёва:
# Центр=894, Ботаника=902, Рышкановка=900, Чеканы=12900,
# Буюканы=12885, Телецентр=13859, Скулянка=12912
# ============================================================
PRICE_999_MIN = 150 # € от
PRICE_999_MAX = 300 # € до
DISTRICTS_999 = "894,902" # районы через запятую
URL_999 = (
"https://999.md/ru/list/real-estate/apartments-and-rooms"
"?appl=1&applied=1"
"&ef=2203,32,30,6,9441"
"&eo=12912,12885,12900,13859"
"&o_33_1=912"
"&o_2203_795=18895"
"&o_32_9_12900_13859=15667"
"&sort=yes&sort_type=date_desc"
f"&from_6_2={PRICE_999_MIN}&to_6_2={PRICE_999_MAX}&unit_6_2=eur"
"&from_9441_2=200&to_9441_2=400&unit_9441_2=eur"
f"&o_30_241={DISTRICTS_999}"
)
# ============================================================
# ФИЛЬТРЫ MAKLER.MD
# Комнаты: 2802=1 комната, 2803=2 комнаты, 2804=3 комнаты
# Валюта: 5=USD, 2=EUR, 3=MDL
# Районы Кишинёва:
# Центр=1024, Ботаника=1025, Рышкановка=1026,
# Буюканы=1030, Чеканы=1023
# ============================================================
PRICE_MAKLER_MIN = 200 # от
PRICE_MAKLER_MAX = 400 # до
CURRENCY_MAKLER = 5 # 5=USD
ROOMS_MAKLER = 2802 # 1 комната
DISTRICTS_MAKLER = [1024, 1025, 1026, 1030, 1023]
_dist = "".join(f"&district[]={d}" for d in DISTRICTS_MAKLER)
URL_MAKLER = (
"https://makler.md/ru/real-estate/real-estate-for-rent/apartments-for-rent"
f"?list&city[]=28{_dist}"
f"&field_432[]={ROOMS_MAKLER}"
"&field_372[]=2666&field_372[]=2667"
f"&price_min={PRICE_MAKLER_MIN}&price_max={PRICE_MAKLER_MAX}"
f"¤cy_id={CURRENCY_MAKLER}"
"&order=date&direction=desc"
)
# ============================================================
# ПРОЧИЕ НАСТРОЙКИ
# ============================================================
SEEN_IDS_FILE = "seen_ids.txt"
SEEN_IDS_FILE_MAKLER = "seen_ids_makler.txt"
SCROLL_PAUSE_TIME = 1.5
MAX_SCROLLS = 4
PAGE_LOAD_TIMEOUT = 20 # секунд ждать загрузки страницы
LOOP_INTERVAL = 60 # секунд между итерациями
# ============================================================
bot = Bot(token=TELEGRAM_TOKEN)
def make_driver():
chrome_path = ChromeDriverManager().install()
service = Service(chrome_path)
options = Options()
options.add_argument("--headless=new")
options.add_argument("--disable-gpu")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--window-size=1920,1080")
options.add_argument(
"user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/148.0.0.0 Safari/537.36"
)
return webdriver.Chrome(service=service, options=options)
def scroll_page(driver):
for _ in range(MAX_SCROLLS):
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(SCROLL_PAUSE_TIME)
# --- Цена ---
def extract_price(price_str):
digits = ''.join(c for c in price_str if c.isdigit())
return int(digits) if digits else 0
def is_price_acceptable(price_str):
price = extract_price(price_str)
low = price_str.lower()
if '€' in low or 'eur' in low:
result = PRICE_EUR_MIN <= price <= PRICE_EUR_MAX
elif '$' in low or 'usd' in low:
result = PRICE_USD_MIN <= price <= PRICE_USD_MAX
elif any(c in low for c in ('mdl', 'лей', 'lei')):
result = PRICE_MDL_MIN <= price <= PRICE_MDL_MAX
else:
result = False
logging.info(f"Цена '{price_str}' -> {price}, подходит={result}")
return result
# --- Telegram ---
async def send_ads_to_telegram(ad):
if not is_price_acceptable(ad['price']):
logging.info(f"Пропущено (цена): {ad['price']}")
return False
message = f"🏠 {ad['title']}\n💰 {ad['price']}\n🔗 {ad['link']}"
try:
await bot.send_message(chat_id=PRIMARY_CHAT_ID, text=message)
## await bot.send_message(chat_id=SECONDARY_CHAT_ID, text=message)
logging.info(f"Отправлено: {ad['id']}")
return True
except Exception as e:
logging.error(f"Ошибка Telegram: {e}")
return False
# --- Seen IDs ---
def load_seen_ids(path):
try:
with open(path, "r") as f:
ids = set(line.strip() for line in f if line.strip())
logging.info(f"Загружено {len(ids)} ID из {path}")
return ids
except FileNotFoundError:
return set()
def save_seen_id(path, ad_id):
with open(path, "a") as f:
f.write(f"{ad_id}\n")
# --- Парсер 999.md ---
def parse_ads_999():
logging.info("999.md: открываю страницу")
driver = make_driver()
try:
driver.get(URL_999)
wait = WebDriverWait(driver, 10)
wait.until(
EC.presence_of_all_elements_located(
(By.CSS_SELECTOR,
'a.styles_advert__photo__link__SnL_t')
)
)
soup = BeautifulSoup(driver.page_source, 'html.parser')
ads = []
items = soup.select(
'a.styles_advert__photo__link__SnL_t'
)
logging.info(f"999.md: {len(items)} карточек")
for node in items:
try:
href = node.get('href', '')
ad_id = (
href.strip('/')
.split('/')[-1]
.split('?')[0]
)
link = (
href
if href.startswith('http')
else f"https://999.md{href}"
)
# Заголовок
title_node = node.select_one('h4')
title = (
title_node.get_text(strip=True)
if title_node else ''
)
# Цена
price_node = node.select_one(
'span.styles_price__text__VPLPL'
)
price = (
price_node.get_text(strip=True)
if price_node else '0'
)
ads.append({
'id': ad_id,
'title': title,
'price': price,
'link': link
})
except Exception as e:
logging.warning(f"999.md parse error: {e}")
return ads
finally:
driver.quit()
# --- Парсер makler.md ---
def parse_makler_ads():
logging.info(f"makler.md: открываю страницу")
driver = make_driver()
try:
driver.get(URL_MAKLER)
wait = WebDriverWait(driver, PAGE_LOAD_TIMEOUT)
for sel in [
'article.ls-detail_item',
'article[class*="ls-detail"]',
'div.ls-detail article',
'div[class*="listing"] article',
'article',
]:
try:
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, sel)))
logging.info(f"makler.md: карточки найдены по '{sel}'")
break
except TimeoutException:
continue
scroll_page(driver)
soup = BeautifulSoup(driver.page_source, 'html.parser')
finally:
driver.quit()
# Ищем контейнер с карточками
container = soup.find('div', class_='ls-detail')
articles = container.find_all('article') if container else []
if not articles:
articles = soup.select('article[class*="ls-detail"]')
if not articles:
articles = soup.find_all('article')
logging.info(f"makler.md: найдено {len(articles)} карточек")
ads = []
for art in articles:
try:
title_el = (
art.select_one('h3 a') or
art.select_one('h2 a') or
art.select_one('a[class*="title"]') or
art.select_one('a[href*="/ru/real-estate/"]')
)
if not title_el: continue
href = title_el.get('href', '')
ad_id = href.strip('/').split('/')[-1]
link = href if href.startswith('http') else f"https://makler.md{href}"
price_el = (
art.select_one('[class*="price"]') or
art.select_one('[class*="Price"]')
)
price = price_el.get_text(strip=True) if price_el else '0'
ads.append({'id': ad_id, 'title': title_el.get_text(strip=True),
'price': price, 'link': link})
except Exception as e:
logging.warning(f"makler.md: {e}")
logging.info(f"makler.md: итого {len(ads)} объявлений")
return ads
# --- Основной цикл ---
async def main():
logging.info("=== Итерация 999.md ===")
seen_999 = load_seen_ids(SEEN_IDS_FILE)
for ad in parse_ads_999():
link = ad.get("link", "")
match = re.search(r"\d+", link)
# 1. Проверяем, удалось ли вообще найти ID в ссылке
if not match:
logging.warning(f"Не удалось найти ID в ссылке: {link}")
continue
ad_id = ad.get("id", "")
# 2. Проверяем, видели ли мы ИМЕННО ЭТОТ ad_id
if ad_id not in seen_999:
if await send_ads_to_telegram(ad):
# 3. Сохраняем ИМЕННО ad_id, а не ad['id']
save_seen_id(SEEN_IDS_FILE, ad_id)
seen_999.add(ad_id)
logging.info(f"Объявление {ad_id} успешно отправлено и сохранено.")
logging.info("=== Итерация makler.md ===")
seen_makler = load_seen_ids(SEEN_IDS_FILE_MAKLER)
for ad in parse_makler_ads():
if ad['id'] not in seen_makler:
if await send_ads_to_telegram(ad):
save_seen_id(SEEN_IDS_FILE_MAKLER, ad['id'])
seen_makler.add(ad['id'])
async def main_loop():
while True:
try:
await main()
except Exception as e:
logging.error(f"Ошибка в main_loop: {e}", exc_info=True)
logging.info(f"Sleeping {LOOP_INTERVAL}s...")
await asyncio.sleep(LOOP_INTERVAL)
if __name__ == "__main__":
asyncio.run(main_loop())