ProxyHandler處理(代理服務器),使用代理IP,是爬蟲的經常使用手段,一般使用UserAgent 假裝瀏覽器爬取仍然可能被網站封了IP,可是咱們使用代理IP就不怕它封了咱們的IP了html
# 使用代理服務器訪問貓眼 # https://maoyan.com/ from urllib import request,error if __name__ == '__main__': url = "https://baidu.com/" # 1.設置代理地址 proxy = {'http': '218.60.8.83:3129'} # 2.建立ProxyHandler proxy_handler = request.ProxyHandler(proxy) # 3.建立Opener opener = request.build_opener(proxy_handler) # 4.安裝Opener request.install_opener(opener) # 下面再進行訪問url就會使用代理服務器 try: rsp = request.urlopen(url) html = rsp.read().decode() print(html) except error.HTTPError as e: print(e) except Exception as e: print(e)
關於爬蟲使用代理服務器,使用代理IP,就介紹到這裏了,使用代理IP是爬蟲的經常使用手段,好處多多git
<hr>- 本筆記不容許任何我的和組織轉載github