Python2.7 硒错误 [英] Python2.7 selenium errors
本文介绍了Python2.7 硒错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
因此,由于无法使用 py2exe,我最近也从 python 3.5 转移了 python 2.7,但是我在 3.5 上运行了这个脚本,但现在我在 2.7 中遇到错误,如果你们能帮我重温这个问题,太好了.
So I recently transfered too python 2.7 from python 3.5 due to not being able to use py2exe, But I had this script running on 3.5 but now I get an error in 2.7 if you guys could give me a hand and help me reslove this issue, it'd be great.
from selenium import webdriver
import time
import random
print("\n")
user_input = raw_input("Username: ")
##########################################################
path = r"C:\Users\Skid\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(10):
try:
driver.find_element_by_xpath("//*[@id='pnnext']/span[2]").click()
except:
pass
time.sleep(5)
posts2 = driver.find_elements_by_class_name("_Rm")
for post2 in posts2:
print(post2.text)
text_file.write(post2.text + "\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/Skid/PycharmProjects/untitled/2nd.py", line 34, in <module>
text_file.write(post2.text + "\n")
UnicodeEncodeError: 'ascii' codec can't encode character u'\u203a' in position 23: ordinal not in range(128)
推荐答案
尝试在 print(post2.text)
A=post2.text
A=A.encode('ascii','ignore')
而不是 text_file.write(post2.text + "\n")
写 text_file.write(A + "\n")
它对我有用.
要调试下一个按钮问题,请尝试以下代码:
to debug next button issue try the following code:
for n in range(10):
nextbutton = 0
try:
nextbutton = driver.find_element_by_xpath("//*[@id='pnnext']/span[2]")
if nextbutton !=0 :
nextbutton.click()
except: pass
time.sleep(5)
if nextbutton != 0 or n==0:
posts2 = driver.find_elements_by_class_name("_Rm")
for post2 in posts2:
print(post2.text)
A=post2.text
A=A.encode('ascii','ignore')
text_file.write(A + "\n")
这篇关于Python2.7 硒错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文