Python Selenium webdriver 和 unicode
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16823086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium webdriver and unicode
提问by nutship
It's my 2nd day with Selenium 2 library and the pain with Unicode never seem to subside.
这是我使用 Selenium 2 库的第二天,Unicode 的痛苦似乎从未消退。
I'm just doing the most basic operation, want to print the page source:
我只是做最基本的操作,想打印页面源码:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://google.com")
print driver.page_source
Sure enough, I get an error:
果然,我得到一个错误:
UnicodeEncodeError: 'ascii' codec can't encode character u'\u0119' in position 62045:
ordinal not in range(128)
How can I please encode this to utf-8?
我该如何编码utf-8?
采纳答案by jaynp
You have options, based on this similar question.
基于此类似问题,您有多种选择。
You can either convert the source to all ascii losing the Unicode characters in the process.
您可以将源转换为在此过程中丢失 Unicode 字符的所有 ascii。
(driver.page_source).encode('ascii', 'ignore')
(driver.page_source).encode('ascii', 'ignore')
Or, and I think you'll prefer this, you can encode it to utf-8 like this:
(driver.page_source).encode('utf-8').
或者说,我想你会喜欢这个,你可以将其编码为UTF-8这样的:
(driver.page_source).encode('utf-8')。
回答by donrondadon
Instead of print(string), use print(repr(string))to return a printable representation of the object.
而不是print(string),用于print(repr(string))返回对象的可打印表示。

