-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
59 lines (46 loc) · 1.75 KB
/
main.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
import requests
from bs4 import BeautifulSoup
# URL = 'https://auto.ria.com/newauto/marka-peugeot/'
URL = 'https://auto.ria.com/newauto/marka-jeep/'
HEADERS = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
'AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/95.0.4638.69 Safari/537.36',
'accept': '*/*'
} # для отправки заголовков, чтобы сервер не посчитал нас ботами и не забанил
HOST = 'https://auto.ria.com'
def get_html(url, params=None):
r = requests.get(url, headers=HEADERS, params=params)
return r
def get_content(html):
soup = BeautifulSoup(html, 'lxml')
items = soup.find_all('section', class_='proposition')
# print(items)
cars = []
for item in items:
usd_price = item.find('span', class_='green')
uah_price = item.find('span', class_='size16')
city = item.find('span', class_='region')
if usd_price or uah_price or city:
usd_price = usd_price.get_text(strip=True).replace(' ', '')
uah_price = uah_price.get_text(strip=True).replace(' ', '')
city = city.get_text(strip=True).replace(' ', '')
else:
usd_price = 'None price USD'
uah_price = 'None Price UAH'
cars.append({
'title': item.find('h3', class_='proposition_name').get_text(strip=True),
'link': HOST + item.find('a').get('href'),
'usd_price': usd_price,
'uah_price': uah_price,
'city': city,
})
# return cars
print(cars)
def parse():
html = get_html(URL)
if html.status_code == 200:
get_content(html.text)
else:
print('Ебучая ошибка')
parse()