代理IP
- 反爬机制:检测请求的ip在单位时间内,请求访问的次数;超过设定的阈值,服务器拒绝请求的访问;--即封IP
- 反反爬策略:使用代理进行请求的发送;
- 代理:破解封IP的反爬机制;
- 代理服务器:实现IP伪装
- 在本机和web服务器间,搭建了一个中转桥梁,本机不直接发送请求到web服务器;
- 本机向代理服务器发起请求,代理服务器会把请求中转发送给web服务器;web服务器响应的数据,由代理服务器转发给本机;
- 代理作用:
- 可以突破自身IP访问的限制,不会因为请求多被封IP;
- 隐藏自身真实的IP,避免被攻击;
- 国内免费IP代理网站:
- 全网代理IP(免费) :http://www.goubanjia.com/
| 代理名称 | 状态 | 更新速度 | 可用率 | 地址 |
|---|---|---|---|---|
| 无忧代理 | √ | ★ | * | 地址 |
| 66代理 | √ | ★★ | * | 地址 |
| 已关闭 | —— | —— | 地址 | |
| 全网代理 | √ | ★ | * | 地址 |
| 快代理 | √ | ☆ | * | 地址 |
| 代理盒子 | √ | ★★★ | * | 地址 |
| 云代理 | √ | ★ | * | 地址 |
| 已关闭 | —— | —— | 地址 | |
| 免费代理库 | √ | ☆ | * | 地址 |
| 89代理 | √ | ☆ | * | 地址 |
| 西拉代理 | √ | ★★ | * | 地址 |
- 代理IP的类型:选择错了,也会报错
- http:应用到http协议对应的url中;
- https:应用到https协议对应的url中;
- 代理IP的匿名度:
- 透明:web服务器知道该次请求使用了代理,也知道请求对应的真实ip;
- 匿名:知道使用了代理,但不知道真实ip;
- 高匿:不知道使用了代理,也不知道真实ip;

1、检测代理IP是否可用
1)访问代理ip地址,来检测代理ip是否可以能访问;(能够访问,但是不一定能代理成功)
# 检测代理IP是否能访问
import requests
# 访问代理IP地址
get_url = 'http://1.198.177.74:4225'
response = requests.get(get_url)
print(response.text)
print(response.status_code)
if response.status_code == 200:
print(f"该代理IP可用:{get_url}")
else:
print(f"该代理IP不可用:{get_url}")
输出结果:
:4225
200
该代理IP可用:http://1.198.177.74:4225
2)telnet ip地址,查看是否可以telnet成功
import telnetlib
def test_ip(ip,port):
try:
telnetlib.Telnet(ip,port,timeout=2)
print("ip "+ip+":"+port+" yes")
except:
print("ip "+ip+":"+port+" ---")
ip_list = [
"222.74.202.227:9999",
"61.135.185.152:80",
"180.109.124.82:4232",
"1.198.177.74:4225",
"58.218.200.229:12907"
]
for ip_str in ip_list:
ip_arr = ip_str.split(":")
test_ip(ip_arr[0], ip_arr[1])
pass
输出结果:
ip 222.74.202.227:9999 yes
ip 61.135.185.152:80 yes
ip 180.109.124.82:4232 ---
ip 1.198.177.74:4225 yes
ip 58.218.200.229:12907 yes
显示访问IP的地址:
访问的目标HTTP页面:targetUrl = "http://httpbin.org/ip"
访问的目标HTTPS页面:targetUrl = "https://httpbin.org/ip"
3)检查代理IP是否可用?
import requests
import random
import time
http_ip = [
'167.172.184.166:46488',
'112.111.217.125:9999',
'186.248.170.82:53281'
]
for ip_proxy in http_ip:
try:
# ip_proxy = random.choice(http_ip)
proxy_ip = {
'http': ip_proxy,
'https': ip_proxy,
}
print('使用代理的IP:', proxy_ip)
response = requests.get("http://httpbin.org/ip", proxies=proxy_ip, timeout = 10).text
print(response)
print('当前IP有效√√√')
time.sleep(2)
print('******************************************************')
except Exception as e:
print(e.args[0])
print('当前IP无效✖✖✖')
print('******************************************************')
输出结果:
使用代理的IP: {'http': '123.163.121.16:9999', 'https': '123.163.121.16:9999'}
HTTPConnectionPool(host='123.163.121.16', port=9999): Max retries exceeded with url: http://httpbin.org/ip (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPConnection object at 0x00000161E9A40700>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。')))
当前IP无效✖✖✖
******************************************************
712

被折叠的 条评论
为什么被折叠?



