簡體   English   中英

無法使用請求模塊在 python 中代理請求

[英]Cannot proxy requests in python using requests module

我正在嘗試在 python 中構建一個基本的代理檢查器實用程序。 這就是我現在所擁有的:

import requests 
from bs4 import BeautifulSoup
currentip=""
originalip=""
isProxied=False

proxies=["104.236.54.196:8080", "187.62.191.3:61456", "138.204.179.162:44088", "91.216.66.70:32306"]
proxy_count = len(proxies)

url = "https://www.ipchicken.com/"
r = requests.get(url)

def statement():
    global currentip
    global originalip
    print("Current ip is: "+currentip)
    print("Your true ip is: "+originalip)



def main(req):
    global currentip
    soup = BeautifulSoup(req.content, "html.parser")
    html = soup.html
    body = html.body
    font = body.find_all('font')
    ip_container = font[0].b
    ip = ip_container.contents[0]
    currentip=ip

main(r)

originalip=currentip

statement()

print("\n\n")

print("testing proxies...")

print("\n\n")

for x in range(proxy_count):
    proxyContainer={"http":"http://"+proxies[x]}
    r2 = requests.get(url, proxies=proxyContainer, timeout=20)
    print("proxy: " + proxies[x])
    main(r2)
    statement()
    print("\n\n")
    if (currentip==originalip): 
        print("Proxy failed.")
    else:
        print("This proxy works")
    print("\n")

代碼運行良好並且發出了請求,但它們似乎沒有被代理。 這是我的輸出:

Current ip is: 
199.229.249.163
Your true ip is: 
199.229.249.163



testing proxies...



proxy: 104.236.54.196:8080
Current ip is: 
199.229.249.163
Your true ip is: 
199.229.249.163



Proxy failed.


proxy: 187.62.191.3:61456
Current ip is: 
199.229.249.163
Your true ip is: 
199.229.249.163



Proxy failed.


proxy: 138.204.179.162:44088
Current ip is: 
199.229.249.163
Your true ip is: 
199.229.249.163



Proxy failed.


proxy: 91.216.66.70:32306
Current ip is: 
199.229.249.163
Your true ip is: 
199.229.249.163



Proxy failed.

我已經在一個單獨的程序中測試了這些代理,它們似乎工作正常,我不認為代理是問題所在。

如果您連接到加密的 url https那么您必須為https連接設置代理,但您僅為http設置代理,因此它不使用代理。

問題是找到工作代理。

我從https://hidemy.name/en/proxy-list/?type=s#list 獲取,但我不知道它會工作多久。

為了測試 IP,我使用了 httpbin.org,它以 JSON 形式返回數據,因此很容易顯示或轉換為 Python 的字典。

import requests 

url = "https://httpbin.org/ip"

proxies = {
   #"http": '141.125.82.106:80',
   "https": '141.125.82.106:80',
}

r = requests.get(url, proxies=proxies)

print(r.text)

ip = r.json()["origin"]
print('IP:', ip)

順便說一句:其他問題可能是某些代理在額外的標頭中發送您的 IP,而服務器可能會得到它 - 所以並非所有代理都是匿名的。


編輯:帶有https://www.ipchicken.com/ 的版本

import requests 
from bs4 import BeautifulSoup

def get_ip(request):
    soup = BeautifulSoup(request.content, "html.parser")
    return soup.find('font').b.contents[0]

url = "https://www.ipchicken.com/"

proxies = {
   #"http": '141.125.82.106:80',
   "https": '141.125.82.106:80',
}

r = requests.get(url, proxies=proxies)
ip = get_ip(r)
print(ip)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM