Soerrrrrr
@Soerrrrrr
да, только в 2020 я начал учить программирование)

При прасинге страницы некорректно отображается кирилица. Как исправить?

При парсинге сайта весь текст что на кирилице отображается в таком виде "Ñ\x80еÑ\x81Ñ\x82айлинг".
если местами и удается заменить это на что-то читабельное, но местами это невозможно....

Как исправить ?

И да ... опять же.. при выгрузке в .csv то что я указываю кирилицей тоже отображается на "клингонском языке"

import requests
from bs4 import BeautifulSoup
import csv

URL = "https://cars.av.by/subaru"
HEADERS = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36" , "Accept": "*/*"}
FILE = "cars.csv"

def get_html(url, params=None):
    r = requests.get(url, headers=HEADERS,)
    return r


def get_pages_count(html):
    soup = BeautifulSoup(html, "html.parser")
    pagination = soup.find_all("li",class_="pages-arrows-index")
    if pagination:
        return int(soup.find("li",class_="pages-arrows-index").get_text().replace("1 из ",""))
    else:
        return 1


def get_content(html):
    soup = BeautifulSoup(html, "html.parser")
    items = soup.find_all('div', class_="listing-item")
    cars = []
    for item in items:
        cars.append({
            "title":    item.find("div", class_="listing-item-title").find("a").get_text().replace("\n                            ","").replace("                        ","").replace("(Ñ\x80еÑ\x81Ñ\x82айлинг)","(рестайлинг)"),
            "link":     item.find("div", class_="listing-item-title").find("a").get("href"),
            "bny":      item.find("div", class_="listing-item-price").find("strong").get_text().replace("Ñ\x80.","бел.руб"),
            "usd":      item.find("div", class_="listing-item-price").find("small").get_text() + " $",
            #"сity":     item.find("div", class_="listing-item-location").find("p").get_text(), ТУТ ХЗ ЧЕГО 
        })
    return cars


def save_files(items, path):
    with open(path, "w", newline="", encoding='utf-8') as file:
        writer = csv.writer(file, delimiter=";")
        writer.writerow(["Марка", "Ссылка", "Цена в BNY", "Цена в $"])
        for item in items:
            writer.writerow([item["title"], item["link"], item["bny"], item["usd"]])


def parse():
    html = get_html(URL)
    print(html.url)
    if html.status_code == 200:
        cars = []
        pages_count = get_pages_count(html.text)
        for page in range(1, pages_count + 1):
            #html = get_html(URL, params={"page":page})
            html = get_html(URL + f'/page/{page}')
            print(f"Парсинг страницы {page} из {pages_count}...{html.url}")
            cars.extend(get_content(html.text))

        save_files(cars, FILE)
        print(cars)
        print(f'Получено {len(cars)} автомобилей')
    else:
        print("Error")
parse()
  • Вопрос задан
  • 217 просмотров
Решения вопроса 1
hottabxp
@hottabxp Куратор тега Python
Сначала мы жили бедно, а потом нас обокрали..
Вот так можно получить "читаемый" html:
url = 'https://cars.av.by/subaru'
response = requests.get(url,headers=headers)
response.encoding = response.apparent_encoding
print(response.text)

Только вам в парсере нужно подкорректировать строки вида:
replace("1 из ",""))
заменить строки вида '1 из' на русские.
Ответ написан
Пригласить эксперта
Ответы на вопрос 2
HemulGM
@HemulGM Куратор тега Python
Delphi Developer, сис. админ
UTF8 кодировка потому что
Ответ написан
@neck0081
software engineer
Перед выводом item в csv добавь
.encode('latin1').decode('utf8')
Иногда помогает

writer.writerow([item["city"].encode('latin1').decode('utf8')])
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы