Веб-парсинг с Python - вывод содержит лишние символы - PullRequest
0 голосов
/ 18 июня 2020

Я новичок в python и изучаю веб-сканирование на курсе udemy. Я пытаюсь очистить некоторый вывод с демонстрационного сайта, и хотя я могу получить результаты, похоже, есть некоторые символы кода, которые я не могу преобразовать в обычный текст.

#!/usr/bin/env python3.6
'''
webscraping html, webpage data.
1. get the authors of quotes on first page.
2. create a list of all the quotes on first page.
3. extract the top ten tags on the home page.
'''
import bs4, requests, urllib
from bs4 import BeautifulSoup

base_url = 'http://quotes.toscrape.com/'
with urllib.request.urlopen(base_url) as response:
    html = response.read()
    text = str(html)
    soup = BeautifulSoup(text, 'lxml')

def get_author():
    authors = set()
    for name in soup.select('.author'):
        authors.add(name.text)
    print(authors)

def get_quotes():
    quotes = []
    for quote in soup.select('.text'):
        quotes.append(quote.text)
    print(quotes)

def top_ten_tags():
    toptags = []
    for tags in soup.select('.tag-item'):
        toptags.append(tags.text)
    print(toptags)


get_author()
get_quotes()
top_ten_tags()

Вывод:

{'Albert Einstein', 'Jane Austen', 'Thomas A. Edison', 'J.K. Rowling', 'Andr\\xc3\\xa9 Gide', 'Steve Martin', 'Eleanor Roosevelt', 'Marilyn Monroe'}
['\\xe2\\x80\\x9cThe world as we have created it is a process of our thinking. It cannot be changed without changing our thinking.\\xe2\\x80\\x9d', '\\xe2\\x80\\x9cIt is our choices, Harry, that show what we truly are, far more than our abilities.\\xe2\\x80\\x9d', '\\xe2\\x80\\x9cThere are only two ways to live your life. One is as though nothing is a miracle. The other is as though everything is a miracle.\\xe2\\x80\\x9d', '\\xe2\\x80\\x9cThe person, be it gentleman or lady, who has not pleasure in a good novel, must be intolerably stupid.\\xe2\\x80\\x9d', "\\xe2\\x80\\x9cImperfection is beauty, madness is genius and it's better to be absolutely ridiculous than absolutely boring.\\xe2\\x80\\x9d", '\\xe2\\x80\\x9cTry not to become a man of success. Rather become a man of value.\\xe2\\x80\\x9d', '\\xe2\\x80\\x9cIt is better to be hated for what you are than to be loved for what you are not.\\xe2\\x80\\x9d', "\\xe2\\x80\\x9cI have not failed. I've just found 10,000 ways that won't work.\\xe2\\x80\\x9d", "\\xe2\\x80\\x9cA woman is like a tea bag; you never know how strong it is until it's in hot water.\\xe2\\x80\\x9d", '\\xe2\\x80\\x9cA day without sunshine is like, you know, night.\\xe2\\x80\\x9d']
['\\n            love\\n            ', '\\n            inspirational\\n            ', '\\n            life\\n            ', '\\n            humor\\n            ', '\\n            books\\n            ', '\\n            reading\\n            ', '\\n            friendship\\n            ', '\\n            friends\\n            ', '\\n            truth\\n            ', '\\n            simile\\n            ']

Как видите, набор авторов должен иметь имя «Андре Жид» с умлаутом, и по какой-то причине python не печатает его. Для второго списка, который представляет собой цитаты, он печатает символы кода, которые я не понимаю. Кто-нибудь может сказать мне, что я здесь делаю не так?

1 Ответ

1 голос
/ 18 июня 2020

Ваша проблема заключается в принудительном преобразовании текста HTML в строку вместо правильного его декодирования:

text = html.decode("utf8")
soup = BeautifulSoup(text, 'lxml')
get_author()
#{... 'André Gide', 'Eleanor Roosevelt'...}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...