Stanislavs Davidovics
Stanislavs Davidovics

Reputation: 11

Python: Object of type 'bytes' is not JSON serializable

I just started programming Python. I want to create a bot. This is my code. Everything works fine till while RUNNING == True:. After that enter profile and it shows error. Additional information so I could work with Firefox i was required to download geckodriver! Any help or suggestions where to look would be appreciated.

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()

Upvotes: 1

Views: 3450

Answers (1)

pjz
pjz

Reputation: 43107

You are opening the file in binary mode; you want text mode, so use open(uafile, 'r').

Also, a bit of unsolicited code review:

while RUNNING == True:

is the same as:

while RUNNING:

Upvotes: 1

Related Questions