После парсинга и сохранения результатов в файл таблиц .csv, вместо букв у меня иероглифы, скриншот прикрепил.
Код
import requests
from bs4 import BeautifulSoup
import csv
HOST = 'https://nsk.online24.market'
URL = 'https://nsk.online24.market/category/elektronika/telefony-i-gadzhety/mobilnye-telefony/'
FILE = 'jobs.csv'
HEADERS = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/96.0'
}
def get_html(url, params=''):
r = requests.get(url, headers=HEADERS, params=params)
return r
def get_content(html):
soup = BeautifulSoup(html, 'html.parser')
items = soup.find_all('div', class_='products__item products__item-flying')
jobs = []
for item in items:
jobs.append({
'title': item.find('span', class_='products__item-info-name').get_text(strip=True),
'link': HOST + item.find('a').get('href'),
'price': item.find('div', class_='products__price-new').get_text(strip=True),
'stock': item.find('div', class_='products__available-in-stock').get_text()
})
return jobs
def save_data(items, path):
with open(path, 'w', newline='', encoding="utf-8") as file:
writer = csv.writer(file, delimiter=';')
writer.writerow(['Название', 'Ссылка', ' Цена', 'Наличие'])
for item in items:
writer.writerow([item['title'], item['link'], item['price'], item['stock']])
def parse():
pages = input('Укажите кол-во страниц ')
pages = int(pages.strip())
html = get_html(URL)
if html.status_code == 200:
jobs = get_content(html.text)
for page in range(1, pages):
print('wait')
html = get_html(URL, params={'page': page})
jobs.extend(get_content(html.text))
save_data(jobs, FILE)
else:print('Error')
parse()