2016-01-23 37 views
0

所以,我总是写在PyCharm运行我的程序,但是当我在reglaur蟒蛇打开它,它给了我这个错误。(它的工作原理pycharm)Python的硒errorss

它看起来像你的文章大多是码;请添加更多详细信息。2/ short

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> 
+0

请更正标题。 – kame

+0

@ kame好吧,我已经修复它通过降级太python 2.7,但是我有一个新的问题.. http://stackoverflow.com/questions/34972965/python2-7-seleuim-errors – Skid

回答

0

最有可能是问题的根源是代码页。 PyCharm在带有Unicode代码页的控制台中运行程序,而独立运行显然可以在代码页437(存在于20世纪80年代)的控制台中运行。 Unicode代码页包含一个代码为#203a的字符,但代码页437没有,因为它只包含256个字符。

因此,要打印字符串的代码高于FF的字符,您必须对您的字符串进行编码/解码或更改您的控制台代码页。 但是,请注意,这两种方法都会带来很多问题。

更好的选择只是坚持使用Unicode,并且从不打印到非Unicode控制台。

+0

我真的不明白你的意思,我对python相当陌生。你能再解释一下吗? – Skid

+0

https://en.wikipedia.org/wiki/Code_page_437 https://en.wikipedia.org/wiki/Unicode https://en.wikipedia.org/wiki/UTF-8 的https: //docs.python.org/2/howto/unicode.html https://docs.python.org/2/library/codecs.html –