O
O
Oleg Petrov2018-11-06 11:11:38
Python
Oleg Petrov, 2018-11-06 11:11:38

How to implement random proxy selection before each request?

I have an api request code

def get_weathermap(self):
        try:
            rq=requests.get(f'api.openweathermap.org/data/2.5/weather?lat=35&lon=139')
            self.is_200(rq)

            res=rq.json()#json.loads(rq.text)
            self.is_success_api(res)
        except:
            print('EXCEPTION: get_weathermap >',sys.exc_info())

Here I found instructions on how to do it through a proxy
https://stackoverflow.com/questions/8287628/proxie...
If I understand correctly, then you need to do this
def get_weathermap(self):
        try:
            http_proxy = "http://108.61.209.46:3128"
            https_proxy = "https://108.61.209.46:3128"
            ftp_proxy = "ftp://108.61.209.46:3128"

            proxyDict = {
                "http": http_proxy,
                "https": https_proxy,
                "ftp": ftp_proxy
            }
            rq=requests.get(f'api.openweathermap.org/data/2.5/weather?lat=35&lon=139', proxies = proxyDict)
            self.is_200(rq)

            res=rq.json()#json.loads(rq.text)
            self.is_success_api(res)
        except:
            print('EXCEPTION: get_weathermap >',sys.exc_info())

How can I mix proxies before each such request if I have a lot of them?
I want to implement a random proxy before each request, and at the same time, if the request was unsuccessful, then again choose from the random one. How to enter a check for the success of the request?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2018-11-06
@NeiroNx

Great random:

import random
random.seed()
proxys = ["p1.proxy.ru","p2.proxy.ru","p3.proxy.ru"]
proxy = random.choice(proxys)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question