Reputation: 49
Edited & added code below: My main problem is i have proxy in file 1 & user agent file 2 & i am changing them both at the same time but now i want to randomly select change both of them & send them to profile.set_preference() but it's only selecting 1 proxy randomly then uses that same proxy everytime. Please someone help me how can i select random proxy as well as user agent, split the proxy then send them to profile ?
def change():
fi = open("C:\\UsersDesktop\\text_file1.txt","r")
file1 = random.choice(fi.readlines())
print(file1)
fi2 = open("C:\\Users\\Desktop\\text_file2.txt","r")
file2 = fi2.read().splitlines()
for p, a in zip(file, file2):
print(p)
IP,PORT = urls.strip().split(':')
print(file1)
try:
print("Trying proxy {0}" .format(p))
print("Brwoser Agent {0}" .format(a))
profile = webdriver.FirefoxProfile()
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.http", IP)
profile.set_preference("network.proxy.http_port", int(PORT))
profile.set_preference("network.proxy.ssl", IP)
profile.set_preference("network.proxy.ssl_port", int(PORT))
profile.set_preference("network.proxy.ftp", IP)
profile.set_preference("network.proxy.ftp_port", int(PORT))
profile.set_preference("general.useragent.override",'{0}'.format(a))
profile.update_preferences()
driver = webdriver.Firefox(firefox_profile=profile)
#driver.set_window_position(-2000, 0)
driver.get("https://www.whatismyiaddress.com")
print('Program will pause for 1 min 40 sec')
time.sleep(100)
driver.quit()
except:
print('This is not working : %s' % p)
print("next try in 5 seconds")
time.sleep(5)
driver.quit()
print('Nothing left to try')
Upvotes: 0
Views: 92
Reputation: 2963
I will provide an example on outputting a randomized list with some explanation.
from random import shuffle
lineList = open("test.txt", "r").readlines()
shuffle(lineList)
for line in lineList:
print(line)
First, we need to import the shuffle method from the random library.
from random import shuffle
Next, we need to read the entire file and store it into a variable.
lineList = open("test.txt", "r").readlines()
We are going to use the readlines() method, because this will give us a list that we can store in the lineList variable to manipulate later.
To randomize the order of the items in our list, we will use the shuffle method that we imported earlier in the program.
shuffle(lineList)
Finally, we are going to iterate through each line in our list using a for loop. We can then manipulate the line as we see fit (in this example, I am simply outputting the variable).
for line in lineList:
print(line)
It is important to note, that each time this program runs, it will create a different randomized list. If you wanted a randomized list, but with the same randomization on each execution of the program, you will have to set the seed of the shuffle random number generator used in the application. More information on how seeding and randomization works can be found here.
Upvotes: 1
Reputation: 101
Here so far I understood the problem:
import random
with open('l.txt', 'r') as f:
res = f.readlines()
text_list = list(res)
for r in range(0, len(text_list)):
random.shuffle(text_list)
line = text_list.pop()
print (len(text_list))
print (line)
Upvotes: 0