Python爬蟲教程-11-proxy代理IP,隱藏地址(貓眼電影)

Python爬蟲教程-11-proxy代理IP,隱藏地址(貓眼電影)

ProxyHandler處理(代理服務器),使用代理IP,是爬蟲的經常使用手段,一般使用UserAgent 假裝瀏覽器爬取仍然可能被網站封了IP,可是咱們使用代理IP就不怕它封了咱們的IP了html

獲取代理IP的的網站:

  • www.goubanjia.com
  • www.xicidaili.com
  • 使用代理來隱藏真實訪問中,代理也不容許頻繁訪問某一個固定網站,因此,代理必定要不少不少
  • 須要使用,從上面的網站拷貝

基本使用步驟:

# 使用代理服務器訪問貓眼
# 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

更多文章連接:Python 爬蟲隨筆

<hr>- 本筆記不容許任何我的和組織轉載github

相關文章
相關標籤/搜索