proxise
proxies的格式是一个字典:{‘http’: ‘http://42.84.226.65:8888‘}
有http与https两种,在爬取不同网站时我们需要选用不同类型的网站时选用不同的proxise,在不知道网站类型时可以将两种类型均放进去,requests会自动选择合适的
proxies = {
"http": "http://10.10.1.10:3128",
"https": "http://10.10.1.10:1080"
}
- http型:{‘http’: ‘http://42.84.226.65:8888‘}
- https型:{‘https’: ‘http://124.193.37.5:8888‘}
如果你是这样的
proxies = {
"http": "http://10.10.1.10:3128"
}
而你要爬的是https型网站
requests请求可以成功,但是请求使用的将会是你真实的ip地址
如果你是这样的
proxies = {
"https": "http://10.10.1.10:1080"
}
而你要爬的是http型网站
你的requests也可以成功,但是使用的还是你真实的ip地址
只有当你的proxies类型与你想访问的网站类型相同,代理ip才会起作用
可以用以下代码检验你的代理ip是否成功启用
import requests
proxies = {
"https": "http://10.10.1.10:1080"
}
req = requests.get('http://icanhazip.com/', proxies=proxies)
print(req.content)
访问 http://icanhazip.com/(https://icanhazip.com/) 可以得到你访问时的ip地址
判断代理是否有效的方法
import telnetlib
try:
telnetlib.Telnet('10.10.1.10', port='1080', timeout=3)
except:
print('ip无效!')
else:
print('ip有效!')

本文介绍了如何正确设置和使用代理IP进行网页爬取,包括不同类型的代理配置方式及其对实际请求的影响,并提供了验证代理IP有效性的方法。
2633

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



