Программа не выдает никаких ошибок, но тем не менее ничего не выводит в консоль и не создает нужные файлы при парсинге, в чем может быть проблема?
import csv
import json
import os.path
import os
import time
import requests
from bs4 import BeautifulSoup
from datetime import datetime
def get_all_pages():
headers = {
'user-agent' :'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36'
}
r = requests.get(url='https://rozetka.com.ua/search/?page=1&producer=gazer&seller=rozetka&text=Gazer')
if not os.path.exists("data"):
os.mkdir("data")
with open("data/page_1.html", "w") as file:
file.write(r.text)
with open("data/page_1.html") as file:
src = file.read()
soup = BeautifulSoup(src, "html.parser")
pages_count = 20
for i in range(1,pages_count + 1):
url = f'https://rozetka.com.ua/search/?page={i}&producer=gazer&seller=rozetka&text=Gazer'
r = requests.get(url = url)
with open(f'data/page_{i}.html','w') as file:
file.write(r.text)
time.sleep (2)
def collect_data(pages_count):
with open(f"parcerRozetka_{cur_date}.csv", "w") as file:
writer = csv.writer(file)
writer.writerow(
(
"Дата",
"Артикул",
"Цена",
'Наличие'
)
)
data = []
cur_date = datetime.now().strftime("%d_%m_%Y")
for page in range (1, pages_count):
with open (f'data/page_{page}.html') as file: src = file.read()
soup = BeautifulSoup(src,'lxml')
items_card = soup.find_all('div', class_='goods-tile__inner')
for item_card in soup.find_all('li', class_='catalog-grid__cell catalog-grid__cell_type_slim ng-star-inserted'):
product_article = item.find('span', class_='goods-tile__title').text
product_price = item.find('p', class_='ng-star-inserted').text.strip()
product_nal = item.find('div', class_='goods-tile__availability goods-tile__availability--available ng-star-inserted').text
data.append ({
"Data": cur_date,
"product_article": product_article,
"product_price": product_price,
"product_nal": product_nal
})
with open(f"parcerRozetka_{cur_date}.csv", "a") as file:
writer = csv.writer(file)
writer.writerow ((
cur_date,
product_article,
product_price,
product_nal
))
print (f'[info] Обработана страница {page}/20')
with open(f'parcerRozetka{cur_date}.json', 'a') as file:
json.dump(data, file, indent = 3, ensure_ascii = False)
def main():
get_all_pages()
collect_data(pages_count=pages_count)
if __name__ == '__main__':
main()