我试图在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.
我已经在一个单独的程序中测试了这些代理,它们似乎工作得很好,我不认为代理是问题。 问题来源StackOverflow 地址:/questions/59379384/cannot-proxy-requests-in-python-using-requests-module
如果你连接到加密的url https,那么你必须为https连接设置代理,但你只为http设置代理,所以它不使用代理。 问题是找到工作代理。 我从https://hidemy.name/en/proxy-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和服务器可能得到它-所以不是所有的代理都是anonymouse。 编辑:版本与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)
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。