Python Forum

Full Version: Fake UserAgent Issue NEED HELP !
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This opens new fake useragent everytime but rondom useragent chooses alot of very old and not used user agents. How can I import from my useragent list as txt in here. I'm beginner level so I have very little knowladge sorry.

import time,sys,random,string,requests,hashlib,json,re
from pprint import pprint
import pymysql as MySQLdb
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

from fake_useragent import UserAgent
ua = UserAgent()
adultsitem Wrote:I'm beginner level so I have very little knowladge sorry.
You should still try something an post that code,as reading a file has a lot of tutorial out there Wink

Let's say have a ua.txt file like this.
Output:
user-agent=Mozilla/5.0 1111 user-agent=Mozilla/5.0 2222 user-agent=Mozilla/5.0 3333
Read and make a list,then can eg choice a random from that list.
import random

with open('ua.txt') as f:
    ua = [i.strip() for i in f]

first = ua[0]
rand_ua = random.choice(ua)
print(first)
print(rand_ua)
Output:
user-agent=Mozilla/5.0 1111 user-agent=Mozilla/5.0 3333