Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.8 kB
1
Indexable
Never
import csv
import requests
from bs4 import BeautifulSoup
 

with open("res.csv", "w", encoding="utf-8-sig", newline="") as file:
    writer = csv.writer(file, delimiter=";")
    writer.writerow([
        "Наименование", "Артикул", "Бренд", "Модель", "Наличие", "Цена", "Старая цена",
        "Ссылка на карточку с товаром"])

url = "https://parsinger.ru/html/index1_page_1.html"
 

def get_html(url):
    response = requests.get(url=url)
    response.encoding = "utf-8"
    return BeautifulSoup(response.text, "lxml")

 
def get_soup(cart):
    soup = get_html(cart).find("div", class_="description")
    return [
        soup.find(id="p_header").text,
        soup.find(class_="article").text.split(":")[1].strip(),
        soup.find(id="brand").text.split(":")[1].strip(),
        soup.find(id="model").text.split(":")[1].strip(),
        soup.find(id="in_stock").text.split(":")[1].strip(),
        soup.find(id="price").text,
        soup.find(id="old_price").text,
        cart,
        ]
 

soup = get_html(url)
nav = ["https://parsinger.ru/html/"+i["href"] for i in soup.find("div", class_="nav_menu").find_all("a")]
urls = [
    "https://parsinger.ru/html/"+i["href"]
    for item in nav
    for i in get_html(item).find("div", class_="pagen").find_all("a")
]

carts = [
    "https://parsinger.ru/html/"+i.find("a")["href"]
    for item in urls
    for i in get_html(item).find_all("div", class_="sale_button")
]
descriptions = [get_soup(cart) for cart in carts]

for description in descriptions:
    file = open("res.csv", "a", encoding="utf-8-sig", newline="")
    writer = csv.writer(file, delimiter=";")
    writer.writerow(description)
file.close()
print("Файл res.csv создан")