Форум сайта python.su
Всем привет! Есть вот такая программка, которая парсит товары по ссылке:
import requests import csv from bs4 import BeautifulSoup as bs headers = {'accept': '*/*', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0'} MAIN_URL = 'https://www.yoox.com' base_url = 'https://www.yoox.com/ru/%D0%B4%D0%BB%D1%8F%20%D0%BC%D1%83%D0%B6%D1%87%D0%B8%D0%BD/%D0%BE%D0%B4%D0%B5%D0%B6%D0%B4%D0%B0/shoponline#/dept=clothingmen&gender=U&season=X&page=1' def yoox_parse(base_url, headers): session = requests.Session() request = session.get(base_url, headers=headers) clothes = [] urls = [] urls.append(base_url) if request.status_code == 200: soup = bs(request.content, 'html.parser') try: pagination = soup.find_all('li', attrs={'class': 'text-light'}) count = int(pagination[-1].text) for i in range(1,count): url = f'https://www.yoox.com/ru/%D0%B4%D0%BB%D1%8F%20%D0%BC%D1%83%D0%B6%D1%87%D0%B8%D0%BD/%D0%BE%D0%B4%D0%B5%D0%B6%D0%B4%D0%B0/shoponline#/dept=clothingmen&gender=U&season=X&page={i}' if url not in urls: urls.append(url) except: pass for url in urls: request = session.get(url, headers=headers) soup = bs(request.content, 'html.parser') divs = soup.find_all('div', attrs={'class': 'col-8-24'}) for div in divs: brand = div.find('div', attrs={'class': 'brand font-bold text-uppercase'}) group = div.find('div', attrs={'class': 'microcategory font-sans'}) old_price = div.find('span', attrs={'class': 'oldprice text-linethrough text-light'}) new_price = div.find('span', attrs={'class': 'newprice font-bold'}) price = div.find('span', attrs={'class': 'fullprice font-bold'}) sizes = div.find_all('span', attrs={'class': 'aSize'}) href = div.find('a', attrs={'class': 'itemlink'}) art = div.find('div', attrs={'class': ''}) if brand and group and new_price: # new_price выводит только товары со скидкой clothes.append({ 'art': art, 'href': MAIN_URL + href.get('href'), 'sizes': [size.get_text() for size in sizes], 'brand': brand.get_text(), 'group': group.get_text(strip=True), 'old_price': old_price.get_text().replace(' ', '').replace('руб', '') if old_price else None, 'new_price': new_price.get_text().replace(' ', '').replace('руб', '') if new_price else None, 'price': price.get_text().replace(' ', '').replace('руб', '') if price else None, }) print(len(clothes)) else: print('ERROR or Done') return clothes def files_writer(clothes): with open('parsed_yoox_man_clothes.csv', 'w', newline='') as file: a_pen = csv.writer(file) a_pen.writerow(('Артикул', 'Ссылка', 'Размер', 'Марка', 'Категория', 'Старая цена', 'Новая цена', 'Цена')) for clothe in clothes: a_pen.writerow((clothe['art'], clothe['href'], clothe['sizes'], clothe['brand'], clothe['group'], clothe['old_price'], clothe['new_price'], clothe['price'])) clothes = yoox_parse(base_url, headers) files_writer(clothes)
Офлайн