2016-01-23 85 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 –