【发布时间】:2018-07-01 11:11:13
【问题描述】:
我正在尝试制作一个小型解析脚本并测试水域。 我不确定为什么会出现此错误
我的代码是
from bs4 import BeautifulSoup as bs
import requests
r = requests.get('http://www.marca.com/en/football/real-madrid.html?intcmp=MENUPROD&s_kw=english-real-madrid')
data = r.text.encode()
soup = bs(data,'html.parser')
print (soup.prettify())
和错误
print (soup.prettify())
UnicodeEncodeError: 'ascii' codec can't encode characters in position 2153-2154: ordinal not in range(128)
但是,如果我在打印行中使用 .encode(),它可以正常工作。 我只想 100% 确定我对此是准确的。有 0 次解析 HTML/XML 的经验
【问题讨论】:
-
你试过不同的解析器吗?
-
应该是检索到的html内容有非unicode字符造成的。
-
通常一个 html 页面必须在其头部声明它使用哪种字符集。尝试获取该字符集名称,然后应用适当的解码器。
-
@Sphinx 就像魔术一样工作。谢谢!
标签: python-3.x beautifulsoup python-requests