Python Deencodeer错误
我总是用PyCharm编写和运行我的程序,但当我用普通python打开它时,它会给我这个错误。。(它在pycharm中工作)Python Deencodeer错误,python,python-3.x,selenium,Python,Python 3.x,Selenium,我总是用PyCharm编写和运行我的程序,但当我用普通python打开它时,它会给我这个错误。。(它在pycharm中工作) 回溯(最近一次呼叫最后一次): 文件“C:\Users\John\Desktop\peopleSearchTool.py”,第32行,在 打印(post2.text) 文件“C:\Users\John\AppData\Local\Programs\Python\Python35-32\lib\encodings\cp437.py”,第19行,在encode中 返回code
回溯(最近一次呼叫最后一次):
文件“C:\Users\John\Desktop\peopleSearchTool.py”,第32行,在
打印(post2.text)
文件“C:\Users\John\AppData\Local\Programs\Python\Python35-32\lib\encodings\cp437.py”,第19行,在encode中
返回codecs.charmap\u encode(输入、自身错误、编码\u映射)[0]
UnicodeEncodeError:“charmap”编解码器无法对位置23中的字符“\u203a”进行编码:字符映射到
问题的根源很可能是代码页。
PyCharm在带有Unicode代码页的控制台中运行您的程序,而独立运行显然在带有代码页437的控制台中运行(该代码页从20世纪80年代开始存在)。
Unicode代码页包含代码为#203a的字符,但代码页437不包含,因为它只包含256个字符
因此,为了打印代码高于FF的字符串,您必须对字符串进行编码/解码或更改控制台代码页。
但是,请注意,这两种方法都可能带来很多问题
更好的选择是坚持使用Unicode,不要打印到非Unicode控制台。我真的不明白你的意思,我对python还相当陌生。你能再解释一下吗?请更正标题。@kame好吧,我已经通过降级python 2.7来修复它,但是我有一个新问题。。
from selenium import webdriver
import time
import random
print("\n")
user_input = input("Username: ")
##########################################################
path = r"C:\Users\John\Desktop\chromedriver.exe"
driver = webdriver.Chrome(path)
##########################################################
text_file = open(user_input + str(random.random()) + ".txt", "w")
text_file.write("GoogleSearch:\n\n")
##########################################################
print("Google results:\n")
driver.get("https://www.google.com/#q=" + user_input)
for n in range(20):
try:
driver.find_element_by_xpath("""//*[@id="pnnext"]/span[2]""").click()
except: print("out of pages")
pass
time.sleep(2)
posts2 = driver.find_elements_by_class_name("_Rm")
for post2 in posts2:
print(post2.text)
text_file.write(post2.text + "\n\n")
print("\n")
print("Pipl results:\n\n")
text_file.write("\n\n")
text_file.write("Pipl results:\n\n")
driver.get("https://pipl.com/search/?q=" + user_input + "&l=&sloc=&in=5")
posts1 = driver.find_elements_by_class_name("line1")
for post1 in posts1:
print(post1.text)
text_file.write(post1.text + "\n")
time.sleep(1)
driver.close()
Traceback (most recent call last):
File "C:\Users\John\Desktop\peopleSearchTool.py", line 32, in <module>
print(post2.text)
File "C:\Users\John\AppData\Local\Programs\Python\Python35-32\lib\encodings\cp437.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u203a' in position 23: character maps to <undefined>