-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasy.py
47 lines (37 loc) · 1.2 KB
/
easy.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
import requests
import csv
import time
from bs4 import BeautifulSoup
enter = 'https://enter.kg'
def get_data(html):
soup = BeautifulSoup(html, 'lxml')
computers = soup.find_all('div', class_='row')
for item in computers:
title = item.find('span', class_='prouct_name').text
price = item.find('span', class_='price').text
image = enter + item.find('img').get('src')
data = {'title': title, 'price': price, 'image': image}
write_to_csv([data['title'], data['price'], data['image']])
def get_total_pages(html):
soup = BeautifulSoup(html, 'lxml')
num = int(soup.find('span', class_='vm-page-counter').text.split()[-1])
return num
def write_to_csv(data):
with open('computers.csv', 'a+') as f:
writer = csv.writer(f)
writer.writerow(data)
def get_html(url):
html = requests.get(url).text
return html
def main():
url = 'https://enter.kg/computers/noutbuki_bishkek'
html = get_html(url)
get_data(html)
num = get_total_pages(html)
for i in range(1, num):
new_url = f'{url}/results,{i}01-{i}00'
new_html = get_html(new_url)
get_data(new_html)
while True:
main()
time.sleep(3600)