问题
有很多时候,我们在用代理IP,搞搞测试,不过很多时候我们都在这么使用,而不知道,到底代理成功没?
注意:
http 只能代理是 http 的网址,https 只能代理是 https 的网址
网上有很多python代理的方法,大部分在代理中写了很多IP,有的还把https页写到里面,我不知道他们后面代码是否添加了判断http还是https网址,代理网址协议不同是不能代理成功的。
本人推荐这么写,只写单个代理,换的时候只需要找到主要信息,拼接出这个格式即可。因为把 http 和 https 分开能够直接访问使用不同协议的网址,方便(那种通用代理IP,既能 http,也能 https的也建议分开)
http_proxies = {"http": "http://114.139.35.220:8118"}
https_proxies = {"https": "https://114.239.254.228:9999"}
环境
编译IDA:pycharm 社区版
python版本:python3.7.4
用到的库:requests、re
第一种方法
最简单的判断是否代理成功,当然是直接使用,是否能够访问同协议网址,能够访问就能使用,反之则不能使用。
测试IP是否可以使用
http: http://www.haoshiwen.org/
https: https://www.gushiwen.org/
这个只测了一次,如果想要准确可以循环多测几次,没有设置时间,只要能够访问就可以用,没追求响应时间。(失败会消耗很多时间,一般建议设置超时 timeout=5~10 就好了)
class TestIP:
"""
测试IP是否可以使用
http: http://www.haoshiwen.org/
https: https://www.gushiwen.org/
"""
@staticmethod
def http(proxies):
"""
http://www.haoshiwen.org/
@param proxies: 代理 http
@return: True/False
"""
url = "http://www.haoshiwen.org/"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0",
"Connection": "close"
}
try:
res = requests.get(url, headers=headers, proxies=proxies)
if res.status_code == 200:
print("代理成功")
return True
except Exception:
print("代理失败")
return False
@staticmethod
def https(proxies):
"""
https://www.gushiwen.org/
@param proxies: 代理 https
@return: True/False
"""
url = "https://www.gushiwen.org/"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0",
"Connection": "close"
}
try:
res = requests.get(url, headers=headers, proxies=proxies)
if res.status_code == 200:
print("代理成功")
return True
except Exception:
print("代理失败")
return False
可以看到有些可以使用,而有的不能使用。
第二种方法
如果上面哪一种你还是不放心,就是想知道 IP 是否真正地被替换了,可以通过访问 IP 查询网站,判断是否替换了 IP。
检查IP是否更换成功
http: http://ip.tool.chinaz.com/
https: https://ipip.net/ip/
这两个是国内比较简洁、高效的 IP 查询网站了。
这个只测了一次,如果想要准确可以循环多测几次,没有设置时间,只要能够访问就可以用,没追求响应时间。(失败会消耗很多时间,一般建议设置超时 timeout=5~10 就好了)
class CheckIP:
"""
检查IP是否更换成功
http: http://ip.tool.chinaz.com/
https: https://ipip.net/ip/
"""
@staticmethod
def MyIP():
"""
https://ipip.net/
@return: 自己的 IP
"""
url = "https://ipip.net/ip/"
headers = {
"Host": "www.ipip.net",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0",
"Connection": "close"
}
res = requests.get(url, headers=headers)
ip_pat = '<input type="text" name="ip" value="(.*?)"'
ip = re.findall(ip_pat, res.text)
print(str(ip) + "\tMyIP")
return ip
@staticmethod
def Http(proxies):
"""
http://ip.tool.chinaz.com/
@param proxies: 代理 http
@return: True/False
"""
url = "http://ip.tool.chinaz.com/"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0",
"Connection": "close"
}
try:
res = requests.get(url, headers=headers, proxies=proxies)
if res.status_code == 200:
ip_pat = '<dd class="fz24">(.*?)</dd>'
ip = re.findall(ip_pat, res.text)
print(str(ip) + "\t代理成功")
return True
except Exception:
print("代理失败")
return False
@staticmethod
def Https(proxies):
"""
https://ipip.net/
@param proxies: 代理 https
@return: True/False
"""
url = "https://ipip.net/ip/"
headers = {
"Host": "www.ipip.net",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0",
"Connection": "close"
}
try:
res = requests.get(url, headers=headers, proxies=proxies)
if res.status_code == 200:
ip_pat = '<input type="text" name="ip" value="(.*?)"'
ip = re.findall(ip_pat, res.text)
print(str(ip) + "\t代理成功")
return True
except Exception:
print("代理失败")
return False
这样就会放心多了吧。
总结
1、http 只能代理是 http 的网址,https 只能代理是 https 的网址。
2、如果用 http 代理 https,其实使用的还是自己的 IP(ipip.net 测试);https 则不能代理 http。
3、免费代理可用的很少(或则几乎不可用),玩玩就好了,费心费力效果不大。
几个还在更新的免费代理
IP海 http://www.iphai.com/free/ng
快代理 https://www.kuaidaili.com/free/
云代理 http://www.ip3366.net/free/
免费代理 http://ip.jiangxianli.com/ # http 最好
西刺代理 https://www.xicidaili.com/ # 综合最好
西拉代理 http://www.xiladaili.com/
齐云代理 http://www.qydaili.com/free/
66免费代理 http://www.66ip.cn/ # 没标明 http/https,但可免费提取 http
89免费代理 http://www.89ip.cn/ # 没标明 http/https 没多大用处
剩余代码
# -*- coding: utf-8 -*-
# @Time : 2019/10/14 22:21
# @Author : ljf
# @File : testIP.py
import requests
import re
if __name__ == "__main__":
http_proxies = {"http": "http://60.173.241.77:8060"}
https_proxies = {"https": "https://117.69.200.180:9999"}
# TestIP.http(http_proxies)
# TestIP.https(https_proxies)
CheckIP.MyIP()
CheckIP.Http(http_proxies)
CheckIP.Https(https_proxies)