【问题标题】:BeautifulSoup parser and cirillic charactersBeautifulSoup 解析器和西里尔字符
【发布时间】:2016-10-02 04:00:49
【问题描述】:

伙计们!

我正在尝试使用 BeautifulSoup 解析此 URL http://mapia.ua/ru/search?&city=%D0%9D%D0%B8%D0%BA%D0%BE%D0%BB%D0%B0%D0%B5%D0%B2&page=1&what=%D0%BE%D0%BE%D0%BE

但我有一个像这样的奇怪字符 �� �1 ���“����”

这是我的代码

from bs4 import BeautifulSoup
import urllib.request

URL = urllib.request.urlopen('http://mapia.ua/ru/search?city=%D0%9D%D0%B8%D0%BA%D0%BE%D0%BB%D0%B0%D0%B5%D0%B2&what=%D0%BE%D0%BE%D0%BE&page=1').read()

soup = BeautifulSoup(URL, 'html.parser')

print(soup.h3.get_text())

谁能帮帮我?

附:我正在使用 python 3

【问题讨论】:

  • 问题是你用来输出数据的shell,我得到ЖЭК №1 ООО "Дуэт",因为我的默认编码是utf-8,你接受的答案实际上导致它不起作用。

标签: python-3.x beautifulsoup


【解决方案1】:

我发现了这个:

import urllib.request
with urllib.request.urlopen('http://python.org/') as response:
   html = response.read()
soup = BeautifulSoup(html.decode('utf-8', 'ignore').encode("utf-8"))

发件人:

How to correctly parse UTF-8 encoded HTML to Unicode strings with BeautifulSoup?

还有:

Delete every non utf-8 symbols froms string

希望对你有帮助;)

【讨论】:

  • 对不起!它没有帮助:(
  • 抱歉忘记最后的 .encode("utf-8"),这将删除所有非 utf8 字符。如果您想要不太具体的字符,则必须通过正则表达式来完成。
猜你喜欢
  • 2017-07-29
  • 1970-01-01
  • 1970-01-01
  • 2018-04-08
  • 1970-01-01
  • 2016-06-15
  • 1970-01-01
  • 2011-12-06
  • 1970-01-01
相关资源
最近更新 更多