这是我使用Selenium 2库的第二天,使用Unicode的痛苦似乎从未平息。
我只是在做最基本的操作,想要打印页面源代码:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://google.com")
print driver.page_source果然,我得到了一个错误:
UnicodeEncodeError: 'ascii' codec can't encode character u'\u0119' in position 62045:
ordinal not in range(128)我怎么才能把这个编码成utf-8呢?
发布于 2013-05-30 04:31:12
您可以根据类似的question进行选择。
您可以将源文件转换为在此过程中丢失Unicode字符的所有ascii。
(driver.page_source).encode('ascii', 'ignore')
或者,我想你会更喜欢这个,你可以像这样把它编码成utf-8:(driver.page_source).encode('utf-8')。
发布于 2018-10-31 19:07:03
使用print(repr(string)) to return a printable representation of the object而不是print(string)。
https://stackoverflow.com/questions/16823086
复制相似问题