简体   繁体   中英

Proxies with Python 'Requests' module (Not Working)

I am trying to create a proxy tester, I have been trying to trouble shoot this for days and can't seem to figure out the issue. Every time I run it, no matter what the proxy is (I've tried many) I get the error

"requests.exceptions.ProxyError: HTTPSConnectionPool(host='advanced.name', port=443): Max retries exceeded with url: /freeproxy (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f9a7e0ea400>: Failed to establish a new connection: [Errno -2] Name or service not known')))"

The message sounds like the proxy is bad but I've tried a boat load that I know work. Here is the code I have

import requests
from requests.exceptions import Timeout

headers = {
    'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:89.0) Gecko/20100101 Firefox/89.0',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Accept-Language': 'en-GB,en;q=0.5',
    'DNT': '1',
    'Connection': 'keep-alive',
    'Upgrade-Insecure-Requests': '1',
}

proxy = {
    "http": "http//132.145.54.142:1080",
    "https": "https//132.145.54.142:1080"

}

proxy_Test = requests.get("https://advanced.name/freeproxy", headers=headers, proxies=proxy, timeout=10)
print(proxy_Test.status_code)
if proxy_Test.status_code == 200:
    print("this proxy is good:" + str(proxy).replace("{", " ").replace("}", "").replace("'", ""))
else:
    print("This proxy is no good")
try:
    x = requests.get("http://www.google.com", headers=headers, proxies=proxy, timeout=10)
except Timeout:
    print("error found")

If you want to hide your ip adress, you can use torrequest instead of requests:

pip install torrequest

Example code ( more here ):

from torrequest import TorRequest

with TorRequest() as tr:
    response = tr.get('http://ipecho.net/plain')
    print(response.text)

    tr.reset_identity()

    response = tr.get('http://ipecho.net/plain')
    print(response.text)

You need to install Tor browser first.

This website is filtered in your country, and the proxy you've set:

proxy = {     "HTTPS": "http://95.66.151.101:8080" }

Did not work either, so you have to find another proxy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM