Python Selenium 页面无法保存源代码编码错误

标签 python selenium unicode

我试图将 Selenium 的源代码保存到 .txt 中,但 .txt 文件保持为空。

当我尝试使用命令打印源代码时:

htmlcode = driver.page_source
(driver.page_source).encode('utf-8'))
print(htmlcode)

它将打印源代码,但随后会终止脚本并出现错误:

File "C:\Python27\lib\encodings\cp850.py", line 12, in encode
return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\u20ac' in position 16329: character maps to <undefined>

最佳答案

问题解决了!经过 3 小时的搜索 ':-)

html = driver.page_source
f = open('savepage.html', 'w')
f.write(html.encode('utf-8'))
f.close()

关于Python Selenium 页面无法保存源代码编码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42155280/

相关文章:

python - 如何修复 : Error uploading file: [WinError 10049] The requested address is not valid in its context

ssl - Selenium Webdriver SSL 证书主机名不匹配问题

mysql - VB.NET、MySQL 和 Unicode

regex - 如何在 Vim 中使用正则表达式搜索字母(ascii 和非 ascii)?

python - 如何增加turtle.tracer()的延迟? [PYTHON]

python - 如何通过python获取ceph的磁盘使用情况

java - selenium.attachFile(String fieldLocator, String fileLocator) 方法

python - 我该如何处理这些弄乱我的打印格式的奇怪特殊字符?

python - 标准化 numpy 数组中的子数组长度以对其进行数学计算

java - 使用 Serenity 运行两次 Selenium 测试