1. 程式人生 > >Python3 使用代理IP,遇到錯誤 [WinError 10061] 由於目標計算機積極拒絕,無法連線。/ Error (MaxRetryError

Python3 使用代理IP,遇到錯誤 [WinError 10061] 由於目標計算機積極拒絕,無法連線。/ Error (MaxRetryError

我的程式碼如下:

# -*- coding: utf-8 -*-
import requests

proxy = "27.42.168.46:48919"
proxies = {
    'http': 'http://' + proxy,
    'https': 'https://' + proxy,
}
User_Agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3"
headers = {'User-Agent': User_Agent}
try:
    print("headers",headers)
    print("proxies",proxies)
    response = requests.get(
            "http://httpbin.org/get",
            proxies=proxies,
            headers=headers,
            timeout=5
    )
    print(response.text)
except requests.exceptions.ConnectionError as e:
    print('Error', e.args)

語法什麼的沒有任何錯誤,跟我之前的一個爬蟲也差不多,就是 headers  和代理地址(proxy )不一樣

但是就是報錯, [WinError 10061] 由於目標計算機積極拒絕,無法連線。

或者報這個錯:

(Error (MaxRetryError("此處省略): Max retries exceeded with url: 此處省略, 'Connection to 119.101.112.9 timed out. (connect timeout=5)'))"),))

然後我換了一個 proxy (代理) 為:  119.101.112.9:9999   然後就 又正常了.

究其原因,是西刺上很多的代理 IP 都不是即時可用的 (在IP的最後一列西刺自己同樣也有驗證時間)

驗證時間越近,IP 可用可能性越大,所以 只有西刺第一頁可用的多一些

而且 代理 IP又分 HTTP 和 HTTPS 型別,HTTP 只能 訪問 HTTP網站不出錯,HTTPS同理

(http 和 https 處理請求的方式不同)