我刚刚开始Python编程。我想创建一个机器人。这是我的代码。
一切正常,直到 while RUNNING == True:
。之后输入配置文件,它显示错误。附加信息以便我可以使用 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()
最佳答案
您正在以二进制模式打开文件;如果您需要文本模式,请使用 open(uafile, 'r')
。
此外,还有一些未经请求的代码审查:
当 RUNNING == True 时:
等同于:
运行时:
关于Python:类型 'bytes' 的对象不可 JSON 序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44054619/