Created
June 21, 2023 02:35
-
-
Save dgicloud/18787ec37eece32ca4176e3a00c035bc to your computer and use it in GitHub Desktop.
try and exception request proxy python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import random | |
| import time | |
| import requests | |
| from requests.exceptions import RequestException | |
| from user_agent import UserAgent | |
| def proxy_scrap(): | |
| url = "https://api.proxyscrape.com/v2/?request=displayproxies&protocol=http&timeout=10000&country=br&ssl=all&anonymity=all" | |
| payload={} | |
| headers = {} | |
| response = requests.request("GET", url, headers=headers, data=payload) | |
| if response.status_code == 200: | |
| return response.text | |
| else: | |
| raise RequestException(f"Erro ao obter lista de proxies. Código de status: {response.status_code}") | |
| def req(domain): | |
| time.sleep(1) | |
| ua = UserAgent() | |
| headers = {'User-Agent': str(ua.random)} | |
| domain = domain.replace('http://', '').replace('https://', '') | |
| api = 'https://rdap.registro.br/domain/' + domain | |
| try: | |
| list_proxy = proxy_scrap() | |
| lista_proxy = list_proxy.split('\n') | |
| proxy = random.choice(lista_proxy) | |
| proxies = {'https': proxy} | |
| print(f"Proxy sendo usado atualmente: {proxy}") | |
| response = requests.get(api, headers=headers, proxies=proxies, timeout=30) | |
| response_json = response.json() | |
| return response_json | |
| except RequestException as e: | |
| print(f"Erro: {e}. Tentando novamente com outro proxy...") | |
| return req(domain) | |
| except ValueError as e: | |
| print(f"Erro: {e}. O servidor pode ter retornado uma resposta inválida. Tentando novamente com outro proxy...") | |
| return req(domain) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment