python - 如何在使用 Python 进行网络抓取时修复西里尔字符
问题描述
我正在使用 BeautifulSoup 用 python 抓取一个西里尔文网站,但我遇到了一些麻烦,每个单词都显示如下:
С¡¡¸Ð»ÑановÑка С°Ð²ÐºÐ¾Ð²Ð° во С°Ð·Ð¸
我还尝试了其他一些西里尔文网站,但它们运行良好。
我的代码是这样的:
from bs4 import BeautifulSoup
import requests
source = requests.get('https://').text
soup = BeautifulSoup(source, 'lxml')
print(soup.prettify())
我应该如何解决它?
解决方案
requests
未能将其检测为utf-8
.
from bs4 import BeautifulSoup
import requests
source = requests.get('https://time.mk/') # don't convert to text just yet
# print(source.encoding)
# prints out ISO-8859-1
source.encoding = 'utf-8' # override encoding manually
soup = BeautifulSoup(source.text, 'lxml') # this will now decode utf-8 correctly
推荐阅读
- arrays - 在 gnuplot 中声明两个数组并相对于另一个绘制它们
- mongodb-.net-driver - 如何从 mongodb 获取特定的嵌入式数组并使用 mongodb c# 驱动程序将其附加到新文档中
- javascript - 我需要简单的逻辑帮助来添加从 ts 文件中的模板获取的数字
- csv - 如何将 csv 文件读入数组,并用另一个 csv 文件中的条目进行比较和替换?
- java - 如何将 a 到 z 转换为二进制
- android - 如何避免firestore中的索引?
- c++ - igraph (c) 访问邻接表的每个元素
- angular - 如何在弹出窗口中显示文本区域内容的innerhtml
- gnuplot - 在 gnuplot 中使用破幂律拟合数据
- android - FFmpeg:如何转换背景模糊的垂直视频