我只是想检索网页,但 HTML 文件中嵌入了一个外来字符。当我使用“查看源代码”时,这个字符不可见。
isbn = 9780141187983
url = "http://search.barnesandnoble.com/booksearch/isbninquiry.asp?ean=%s" % isbn
opener = urllib2.build_opener()
url_opener = opener.open(url)
page = url_opener.read()
html = BeautifulSoup(page)
html #This line causes error.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 21555: ordinal not in range(128)
我也试过...
html = BeautifulSoup(page.encode('utf-8'))
如何在不出现此错误的情况下将此网页读入 BeautifulSoup?
最佳答案
当您尝试打印 BeautifulSoup 文件的表示时,这个错误可能实际上发生了,如果我怀疑您正在交互式控制台中工作,这将自动发生。
# This code will work fine, note we are assigning the result
# of the BeautifulSoup object to prevent it from printing immediately.
from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(u'\xa0')
# This will probably show the error you saw
print soup
# And this would probably be fine
print soup.encode('utf-8')
关于python - urlopen、BeautifulSoup 和 UTF-8 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1320591/