Python BeautifulSoup UnicodeEncodeError:ascii编解码器
我正在尝试使用BeautifulSoup进行一些解析:Python BeautifulSoup UnicodeEncodeError:ascii编解码器,python,beautifulsoup,Python,Beautifulsoup,我正在尝试使用BeautifulSoup进行一些解析: from bs4 import BeautifulSoup import requests import lxml r = requests.get('https://pythonprogramming.net/parsememcparseface/') page_text = r.text.encode('utf-8').decode('ascii', 'ignore') soup = BeautifulSoup(page_text
from bs4 import BeautifulSoup
import requests
import lxml
r = requests.get('https://pythonprogramming.net/parsememcparseface/')
page_text = r.text.encode('utf-8').decode('ascii', 'ignore')
soup = BeautifulSoup(page_text, 'lxml')
print(soup.find_all('p'))
我无法使用
find_all('p')
,因为UnicodeEncodeError
。输入soup.p效果很好。我使用变量page\u text
对html文件进行编码,但这还不够。如何克服此错误并访问站点中的所有段落?我运行您的代码,然后不给出任何错误。我仍然收到此错误print(soup.find_all('p'))UnicodeEncodeError:“ascii”编解码器无法对760位置的字符“\xa0”进行编码:序号不在范围内(128)
我也是,没有错误,即使删除变量page\u text
,并将page\u text
更改为r.content
在beautifulsou
中,它也可以工作。您的print()
失败,而不是您的beautifulsou代码。您正在将Unicode文本打印到不支持字符集的控制台。@MartijnPieters我想您知道了,我正在使用升华文本。知道解决办法吗?