Selenium and SSL Unicode error



我是Selenium和Python的新手,正在尝试通过Firefox启动一个网页并保存该网页的输出。SSL站点不是自签名的,此脚本在非SSL站点上运行良好。Firefox配置文件配置了socks5代理。

import os
from selenium import webdriver
profile = webdriver.FirefoxProfile("./MyProfile")
driver = webdriver.Firefox(firefox_profile=profile)
driver.get('https://www.sslexample.com/')
html = driver.page_source
f = open("test.html", "wt")
f.write(html)
f.close()

我得到的错误是:

Traceback (most recent call last):
  File "C:UsersxxxxDocumentsPythonProgramsst01.py", line 16, in <module>
    f.write(html)
  File "C:UsersxxxxAppDataLocalProgramsPythonPython35libencodingscp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character 'u0100' in position 1842: character maps to <undefined>

我试过玩编码之类的东西,但没有取得任何进展。感谢您提供的任何帮助!

UTF-8写入文件时使用codecs

import os
import codecs
from selenium import webdriver
profile = webdriver.FirefoxProfile("./MyProfile")
driver = webdriver.Firefox(firefox_profile=profile)
driver.get('https://www.sslexample.com/')
html = driver.page_source
with codecs.open('test.html', 'w', 'utf-8') as file_object:
    file_object.write(html)

最新更新