无法使用请求模块在python中代理请求
我正在尝试用python构建一个基本的代理检查器实用程序。这就是我现在拥有的:无法使用请求模块在python中代理请求,python,python-3.x,http,python-requests,proxies,Python,Python 3.x,Http,Python Requests,Proxies,我正在尝试用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(
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
设置代理,因此它不使用代理
问题是如何找到工作代理
我从中拿走了,但我不知道它能用多久
为了测试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,服务器可能会收到它-因此并非所有代理都是匿名的
编辑:带有
如果您连接到加密的url
https
,那么您应该为加密的https
或ssl
连接设置代理-但是您只为http
设置代理,而不是在main()
中使用global
您应该使用返回ip
和currentip=main(r)
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)