我正在尝试抓取这个包含印地语数据的NREGA Website。这个结构很容易被刮掉。但是当我使用request/urllib来获取html代码时,印地语文本被转换成了一些乱码。不过,文本在站点的代码源中显示得很好。
content = requests.get(URL).text站点中的‘1पीएस’被解析为‘1 \xe0\xa4\xaa\xe0\xa5\x80 \xe0\xa4\x8f\xe0\xa4\xb8’,在我尝试导出到csv时显示为乱码。
发布于 2020-09-21 15:01:23
来自服务器的响应没有在其Content-Type头中指定字符集,因此请求assumes that the page is encoded as ISO-8859-1 (拉丁文-1)。
>>> r = requests.get('https://mnregaweb4.nic.in/netnrega/writereaddata/citizen_out/funddisreport_2701004_eng_1314_.html')
>>> r.encoding
'ISO-8859-1'实际上,页面被编码为UTF-8,我们可以通过检查响应的apparent_encoding属性来判断:
>>> r.apparent_encoding
'utf-8'或者通过实验:
>>> s = '1 \xe0\xa4\xaa\xe0\xa5\x80 \xe0\xa4\x8f\xe0\xa4\xb8'
>>> s.encode('latin').decode('utf-8')
'1 पी एस'正确的输出可以通过解码响应的content属性来获得:
>>> html = r.content.decode(r.apparent_encoding)https://stackoverflow.com/questions/63985897
复制相似问题