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

Python 试图通过BeautifulSoup库获取HTML标记时出现UnicodeEncodeError错误,python,web-scraping,Python,Web Scraping,我在尝试从BeautifulSoup库执行“prettify”函数时遇到Unicodeincodeerror 以下是发生的错误: UnicodeEncodeError:“charmap”编解码器无法对位置97中的字符u'\u200b'进行编码:字符映射到 下面是我试图执行的代码块 import urllib2 link = 'https://stackoverflow.com/' history = urllib2.urlopen(link) from bs4 import Beautiful

我在尝试从BeautifulSoup库执行“prettify”函数时遇到Unicodeincodeerror

以下是发生的错误:

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()
试试这个

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'))

如果您想解析stackoverflow,那么就有api可用,只需在第8行进行更改即可。打印ht.prettify().encode('utf-8')谢谢您的帮助。