尝试通过BeautifulSoup库获取HTML标记时出现UnicodeEncodeError错误

时间:2017-07-03 07:34:03

标签: python web-scraping

我在尝试从BeautifulSoup库执行'prettify'函数时遇到UnicodeEncodeError。

以下是发生的错误:

UnicodeEncodeError:'charmap'编解码器无法编码位置97中的字符u'\ u200b':字符映射到

我正在尝试执行的代码块在下面。

import urllib2
link = 'https://stackoverflow.com/'
history = urllib2.urlopen(link)

from bs4 import BeautifulSoup
ht = BeautifulSoup(history,"html.parser")

print ht.prettify()

1 个答案:

答案 0 :(得分:0)

试试这个

from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
html = urlopen("https://stackoverflow.com/questions/44880061/unicodeencodeerror-error-when-trying-to-fetch-html-tags-through-beautifulsoup-li#44880061")
bsObj = BeautifulSoup(html,"lxml")

print (bsObj.prettify().encode('utf-8'))
相关问题