1

How can i load proxies from a txt file in python?

The proxies are the following format:

1.1.1.1:80
1.1.1.2:80

All of them are open on port 80.

Here is what i tried but without success it only gets 1 proxy from the file for some reasons.

import requests

s = requests.session()
proxy = set()
with open("proxies.txt", "r") as f:
    file_lines1 = f.readlines()
    for line1 in file_lines1:
        proxy.add(line1.strip())
        
proxies = {
    'http': 'http://'+line1
    }


r = requests.get('http://www.google.com/',proxies=proxies)

1 Answers1

3

You should use the proxy variable that contains all the proxies. You can use the random module to pick random proxies


import requests, random

s = requests.session()
proxy = set()

with open("proxies.txt", "r") as f:
    file_lines1 = f.readlines()
    for line1 in file_lines1:
        proxy.add(line1.strip())
        
proxies = {
    'http': 'http://'+random.choice(list(proxy))
}


r = requests.get('http://www.google.com/',proxies=proxies)
bigbounty
  • 16,526
  • 5
  • 37
  • 65