Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用bs4解析utf-8编码页中的特殊字符时出现问题_Python_Beautifulsoup_Character Encoding - Fatal编程技术网

Python 使用bs4解析utf-8编码页中的特殊字符时出现问题

Python 使用bs4解析utf-8编码页中的特殊字符时出现问题,python,beautifulsoup,character-encoding,Python,Beautifulsoup,Character Encoding,我试图解析一个页面,但我对特殊字符(如éèá等)有一些问题 根据Firefox页面信息工具,页面编码为UTF-8 我的代码如下: 导入bs4 导入请求 url='1〕https://www.registreentreprises.gouv.qc.ca/RQEntrepriseGRExt/GR/GR99/GR99A2_05A_PIU_AfficherMessages_PC/ActiEcon.html' page=请求.get(url) cae_obj_soup=bs4.BeautifulSoup(

我试图解析一个页面,但我对特殊字符(如éèá等)有一些问题

根据Firefox页面信息工具,页面编码为UTF-8

我的代码如下:

导入bs4
导入请求
url='1〕https://www.registreentreprises.gouv.qc.ca/RQEntrepriseGRExt/GR/GR99/GR99A2_05A_PIU_AfficherMessages_PC/ActiEcon.html'
page=请求.get(url)
cae_obj_soup=bs4.BeautifulSoup(page.text,'lxml',来自_encoding='utf-8')
list_all_domain=cae_obj_soup.find_all('th'))
对于列表\所有\域中的元素:
打印(element.get_text())
输出为:

Pêche et piégeage
Exploitation forestière

我尝试用
iso-8859-1
(法语编码)和其他一些编码来更改编码,但没有成功。我读了几篇关于解析特殊字符的文章,他们基本上都说这是选择正确编码的问题。是否有可能我无法正确解码某个特定网页上的特殊字符,或者我做错了什么?

请求库会处理这些请求。另一方面,BeautifulSoup具有确定文本编码的强大工具。因此,最好将请求的原始响应传递给BeautifulSoup,然后

>r=requests.get('https://www.registreentreprises.gouv.qc.ca/RQEntrepriseGRExt/GR/GR99/GR99A2_05A_PIU_AfficherMessages_PC/ActiEcon.html')
>>>汤=BeautifulSoup(r.含量,'lxml')
>>>list_all_domain=soup.find_all('th'))
>>>[e.在列表\u所有\u域中为e获取\u文本()
[“农业”、“服务业与农业的关系”、“Pèche et piégeage”,…]