web-scraping - Python网页抓取希腊字母未显示
问题描述
我正在尝试学习如何使用 python3 自动执行任务。现在,我正在尝试打开一个网站,从中获取一个元素,然后使用 requests、docx 和 bs4 模块将其文本作为新段落写入单词表。所有这些都很好,但是该网站包含一些希腊字母。当我尝试打开单词表时,数字等很好,但希腊字母出现错误(它们都显示为 Öéëïá 等)。我怎么解决这个问题??这是我的代码:
import requests, docx, bs4
doc = docx.Document()
res=requests.get(“http://www.betcosmos.com/index.php?page=kouponi_stoixima”)
soup =bs4.BeautifulSoup(res.text, “lxml”)
elem =soup.select(“.kouponi_table”)
doc.add_paragraph(elem[0].getText())
doc.save(“BetMasterData.docx”)
在此先感谢您的时间
解决方案
阅读我们遇到的关于响应内容的请求文档。 请求 2.18.4 文档 - 响应内容
回复内容
我们可以读取服务器响应的内容。再次考虑 GitHub 时间线:
导入请求
r = requests.get(' https://api.github.com/events ')
r.text u'[{"repository":{"open_issues":0,"url":" https://github.com/ ...
请求将自动解码来自服务器的内容。大多数 unicode 字符集都是无缝解码的。
当您发出请求时,Requests 会根据 HTTP 标头对响应的编码进行有根据的猜测。访问 r.text 时会使用 Requests 猜测的文本编码。您可以使用 r.encoding 属性找出 Requests 正在使用的编码,并对其进行更改:
r.encoding 'utf-8' r.encoding = 'ISO-8859-1'
如果您更改编码,Requests 将在您调用 r.text 时使用 r.encoding 的新值。您可能希望在任何可以应用特殊逻辑来计算内容编码的情况下执行此操作。例如,HTML 和 XML 能够在它们的正文中指定它们的编码。在这种情况下,您应该使用 r.content 找到编码,然后设置 r.encoding。这将允许您使用正确编码的 r.text。
如果您需要,请求还将使用自定义编码。如果您创建了自己的编码并将其注册到编解码器模块,您可以简单地使用编解码器名称作为 r.encoding 的值,Requests 将为您处理解码。
二进制响应内容
对于非文本请求,您还可以将响应正文作为字节访问:
r.content b'[{"repository":{"open_issues":0,"url":" https://github.com/ ...
gzip 和 deflate 传输编码会自动为您解码。
试试这个:
import requests, docx, bs4
doc = docx.Document()
res = requests.get('http://www.betcosmos.com/index.php?page=kouponi_stoixima')
soup = bs4.BeautifulSoup(res.content, 'lxml')
elem = soup.select('.kouponi_table')
doc.add_paragraph(elem[0].getText())
doc.save('BetMasterData.docx')`
推荐阅读
- permissions - 还有其他方法吗,我在marklogic中以优化的方式检查了用户的权限?
- java - NativeSearchQueryBuilder 按字符串和布尔值搜索
- docker - Docker + Nginx + EMQX
- windows-10 - Autohotkey 未加载 MS Teams
- vba - 在VBA ppt中的ppt注释页面内容中查找重复的字符串
- python - 从整数范围中获取随机整数
- html - 如何使用 CSS 将图像放入粒子爆炸中
- javascript - 更改下拉菜单时不会触发 Javascript
- python - Python 安排一个作业直到截止日期
- xcode - 每个环境的反应本机应用程序显示名称