為什么會用到爬蟲代理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。