-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathavito.py
More file actions
59 lines (40 loc) · 1.44 KB
/
avito.py
File metadata and controls
59 lines (40 loc) · 1.44 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
import requests
from bs4 import BeautifulSoup
MAIN_URL = 'https://www.avito.ru'
def get_html(url):
response = requests.get(url)
return response.text
def parse(html):
arrItem = []
soup = BeautifulSoup(html, 'html.parser')
all_items = soup.find('div', class_='catalog-list')
items = all_items.find_all('div', class_='item_table')
for item in items:
name = item.find('div', class_='description').find('h3', class_='item-description-title').a.get_text()
price = item.find('div', class_='description').find('div', class_='about').contents[0]
link = item.find('div', class_='description').find('h3', class_='item-description-title').a.get('href')
link = 'https://www.avito.ru' + link
arrItem.append({
'title': name,
'price': price,
'link': link
})
return arrItem
def parseLocation(html):
soup = BeautifulSoup(html, 'html.parser')
arrCities = []
objCities = {}
tables = soup.find_all('div', class_='cities')
for table in tables:
cities = table.find_all('a')
for city in cities:
arrCities.append(city)
for item in range(0, len(arrCities)):
name = arrCities[item].get_text().strip('\n ')
link = 'https:' + arrCities[item].get('href')
objCities[name] = link
return objCities
def main():
print(parseLocation(get_html(MAIN_URL)))
if __name__ == '__main__':
main()