我试图将 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/