爬虫中代理IP怎么使用的,失效了怎么办?

为什么会用到爬虫代理IP,代理怎么使用,代理失效了怎么处理",初级的爬虫工作者经常会受到这样的困扰.

为什么会用到代理?

安全避免同一个代理IP访问同一个网页,对于长时间访问同一个网页的IP,极大可能性IP会被封掉。方便解决IP代理问题技术含量高,找代理处理方便省事。成本低自己去维护服务器成本过高,不低于长久持续发展。

代理IP如何使用



代理IP有效性方法:在有限时间内正常访问url即可,如果访问了,证明代理IP是有效的





P代理基本上是这个流程了

代理失效了如何处理?

class urllib.request.ProxyHandler(proxies=None)Cause requests to go through a proxy. If proxies is given, it must be a dictionary mapping protocol names to URLs of proxies.

(通过代理方法请求,如果给定一个代理,它必须是一个字典映射,key为协议,value为URLs或者代理ip。)

urllib.request.build_opener([handler, ...])Return an OpenerDirector instance, which chains the handlers in the order given.

(build_opener方法返回一个链接着给定顺序的handler的OpenerDirector实例。)urllib.request.install_opener(opener)Install an OpenerDirector instance as the default global opener.

(install_opener方法安装OpenerDirector实例作为默认的全局opener。)

简单整理下:1、将代理IP及其协议载入ProxyHandler赋给一个opener_support变量;2、将opener_support载入build_opener方法,创建opener;3、安装opener。

 

猜你喜欢

转载自www.cnblogs.com/xiniudaili/p/10475013.html