-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
61 lines (59 loc) · 2.62 KB
/
Copy pathparser.py
File metadata and controls
61 lines (59 loc) · 2.62 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
# Импорты
import requests, time, os
from bs4 import BeautifulSoup
from datetime import datetime
def scrape(link):
# Юзер Агент
session = requests.Session()
ua = {"User-Agent":"Mozilla/5.0 (Linux; Android 11; M2004J19C) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Mobile Safari/537.36"}
# Проверка на правильность ссылки
if link.find('https://ggsel.com/catalog/') != -1:
dir_name = link.replace('https://ggsel.com/catalog/', '')
# Создаем директории
try:
os.mkdir('data')
except:
pass
try:
os.mkdir(f'data/{dir_name}')
except:
pass
# Получаем "сырую" информацию
raw_data = session.get(link, headers=ua)
# Приводим информацию в нормальный вид
data = BeautifulSoup(raw_data.text, 'lxml')
# Ищем все предложения
find_data = data.find_all('div', class_='product-item')
# Обрабатываем предложения
count = 0
for find in find_data:
count += 1
# Создаем директории
try:
os.mkdir(f'data/{dir_name}/{count}')
except:
pass
full_info = ""
# Описание товара
full_info += "Описание: " + (find.find('a', class_='product-item-descr').text)
# Цена
prices = (find.find('div', class_='product-price'))
full_info += "\nЦена: " + (prices.find('span').text)
# Если есть скидка на товар, записываем
try:
full_info += "\nСтарая цена: " + (prices.find(class_='cost-old').text)
full_info += ("\nСкидка: " + find.find('div', class_='product-item-badge').text).replace(' ', '')
except:
pass
# Ссылка на товар
full_info += "\nСсылка: " + (find.find('a', class_='prod-link')['href'])
# Картинка товара
image = find.find('a', class_='product-item-img lazy')['data-bg']
image = session.get(image, headers=ua).content
with open(f'data/{dir_name}/{count}/{count}.png', 'wb') as f:
f.write(image)
with open(f'data/{dir_name}/{count}/{count}.txt', 'w') as f:
f.write(full_info)
print(f'{count} готово')
link = input('Введите ссылку: ')
scrape(link)