我刚开始编写Python。我想创建一个机器人。这是我的代码。在while RUNNING == True:之前一切都很好。在那之后输入profile,它显示错误。为了能够使用Firefox,我需要下载geckodriver!任何帮助或建议,在哪里寻找将不胜感激。
import random, time, requests
from selenium import webdriver
from selenium.webdriver.common.proxy import *
from bs4 import BeautifulSoup
import selenium.webdriver.chrome.service
import webbrowser
USER_AGENT_FILE = './user_agent.txt'
RUNNING = True
def LoadUserAgents(uafile=USER_AGENT_FILE):
uas = []
with open(uafile, 'rb') as uaf:
for ua in uaf.readlines():
if ua:
uas.append(ua.strip() [1:-1-1])
random.shuffle(uas)
return uas
uas = LoadUserAgents()
while RUNNING == True:
profile = webdriver.FirefoxProfile()
profile.set_preference('general.usragent.override', random.choice(uas))
driver = webdriver.Firefox(firefox_profile=profile)
driver.get('http://whatmyua.com')
input('Press enter to continue')
driver.quit()发布于 2017-05-19 03:25:53
您正在以二进制模式打开该文件;您希望使用文本模式,因此请使用open(uafile, 'r')。
此外,还有一点不请自来的代码审查:
while RUNNING == True:
与以下内容相同:
while RUNNING:
https://stackoverflow.com/questions/44054619
复制相似问题