Python:类型 'bytes' 的对象不可 JSON 序列化

标签 python selenium

我刚刚开始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/

相关文章:

python - 如何在pygame中使物体移动一定角度

Python - 用户生成的字典名称和输入

python - 打开 cv2 模块的 rc.local 导入错误

python - Python 3.7 数据类中的类继承

selenium - go语言自动化测试

python - 为什么any() 比in 快这么多?

python - Selenium - 在计算机之间共享浏览器 session (cookies、本地存储)

selenium - 如何使用 Java 在 Selenium Web 驱动程序中输入类型 =Button 时单击按钮

javascript - 如何通过 NodeJS 和 Mocha 执行具有不同结构的不同测试用例

java - 使用 Selenium 手动下载文件 - PhantomJS