def collect_data():
row = 0
active_sheet = 1
wb = openpyxl.Workbook()
for page in PAGES:
print(f"[INFO] Работа с разделом [{page}]")
wb.create_sheet(page)
wb.active = active_sheet
sheet = wb.active
sheet.cell(row=1, column=1, value="Артикул")
sheet.cell(row=1, column=2, value="Ссылка на товар")
for filenames in os.listdir(f"hardware/{page}"):
with open(f"hardware/{page}/{filenames}", encoding="utf-8") as file:
print(f"[INFO] Собираем данные с файла {filenames}")
content = file.read()
soup = BS(content, "lxml")
products = soup.find_all("li", class_ = "result__item")
for product in products:
row += 1
product_name = product.find("span", class_ = "result__name").text
product_link = product.find("a", class_ = "result__link").get("href")
cell_name = sheet.cell(row=row+1, column=1)
cell_name.value = product_name
cell_link = sheet.cell(row=row+1, column=2)
cell_link.value = product_link
print(f"[SUCCESS] Данные с файла {filenames} успешно считаны!")
print(f"[SUCCESS] Данные с раздела {page} успешно прочитаны!")
wb.save('result.xlsx')
print("[SUCCESS] Данные успешно собраны!")