HTTPX |请求代理设置

问题描述 投票:0回答:2

我正在尝试在我的 WebScraping 项目中使用代理,该项目是我使用 HTTPX 构建的。 然而,当我设置代理时,我仍然被阻止,所以我尝试了它是否真正有效/被使用。我从专业网站/卖家那里购买了一些代理,所以它们工作得很好。

我找到了一个网站,它返回我从中发出请求的IP。 我尝试测试这样的代理的使用:

import httpx
import requests

#Username:PW:Hostname
proxies = {"http://": "http://username:pw.io:hostname"}


#response = requests.get('http://ipinfo.io/json',proxies=proxies)
response = httpx.get('http://ipinfo.io/json',proxies=proxies)

print(response.text)

请求和 httpx 对我都不起作用,因为响应总是返回我的真实 IP。我需要如何设置我的 Proxiex?请记住,我实际上想使用 HTTPX 并且只是使用请求进行调试。

python proxies httpx
2个回答
1
投票

如果尝试先在没有代理的情况下发送验证IP地址的请求(例如使用WhatIsMyIP工具和其他工具),然后通过代理发送请求,怎么样

import httpx

proxy_url = "http://username:pw.io:hostname"
proxies = {"http://": proxy_url, "https://": proxy_url}

# Verify our IP address without proxy first to this website
response = httpx.get("https://www.whatismyip.com/")
print("IP address without proxy:", response.text)

# And then we request through proxy
with httpx.Client(proxies=proxies) as client:
    response = client.get("https://www.whatismyip.com/")
    print("IP address with proxy:", response.text)

如果代理正常工作,显示的IP地址应该与之前的不同


0
投票
import httpx
from free_verify_proxy import VerifyProxyLists

verify_proxy_lists = VerifyProxyLists().get_verifyProxyLists(number_of_threads=200, timeout=(5,5))
print(len(verify_proxy_lists))

for proxy in verify_proxy_lists:
    
    proxies = {"http://": f"http://{proxy}", "https://": f"http://{proxy}"}
    
    try:
        response=httpx.get("https://ipinfo.io/json", proxies=proxies)
        print("IP address with proxy:", response.json())
    except:
        pass
© www.soinside.com 2019 - 2024. All rights reserved.