V
V
VeinKoor2022-01-29 18:28:22
Python
VeinKoor, 2022-01-29 18:28:22

Why does the .cvs file have hieroglyphs instead of letters after python parsing?

After parsing and saving the results to a .csv table file, instead of letters I have hieroglyphs, I attached a screenshot.

61f55cbb4150b423410392.png

The code

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()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
o5a, 2022-01-29
@VeinKoor

For Russian Excel, the default encoding when opening CSV is Windows-1251, so it opens in it. So if you need the ability to open immediately in Excel, then when saving, specify windows-1251 exactly. But the CSV file itself is normal and in its current form, the point is in the very features of Excel.

D
Dr. Bacon, 2022-01-29
@bacon

K - encoding

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question