1. 程式人生 > 其它 >爬蟲---01.requests模組

爬蟲---01.requests模組

  • requests是基於網路請求的模組

    • pip install requests
  • 作用和流程:模擬瀏覽器發起請求

    • 1.指定url

                      url = "https://..."
      
    • 2.發起請求

                      response = requests.get(url=url, headers=headers)
      
    • 3.獲取響應資料(爬取到的頁面原始碼資料)

                      page_text = response.text
      
    • 4.持久化儲存

                      with open('./xx.html', 'w', encoding='utf-8') as fp:
                          fp.write(page_text)
      
    • 5.雪薇進階點就直接解析放入excel或者資料庫

  • url攜帶引數和引數動態化

    • 1.將攜帶的動態引數以鍵值對的影視封裝到一個字典中

    • 2.將該字典作用到get方法的params引數中即可。

    • 3.需要將原始url中攜帶的引數刪除

                     keyWord = input(">>>")
                     params = {"query": keyWord}
                     url = ""
                     response = requests.get(url, params=params)
                     # 注意 在出現亂碼的情況時,需要修改編碼格式
                     response.encoding = "utf-8"
                     page_text = response.text
                     # 注意 params字典值為空,要新增“”空字元,數字要寫成字串
      
                     # 例項 用json是因為資料是物件格式的
                     url = "https://movie.douban.com/j/chart/top_list"
                     params = {"type": "5",
                               "interval_id": "100:90",
                               "action": '',
                               "start": "0",
                               "limit": "20",
                     }
                     headers = {
                         "User-Agent": "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)",
                     }
                     response = requests.get(url=url, params=params, headers=headers)
                     print(response.json())
      
  • User-Agent

    • 請求載體的身份標識(瀏覽器或爬蟲程式就是載體)
    • 瀏覽器的身份標識是固定的 身份標識可以通過抓包工具獲取
    • 爬蟲程式 身份標識是各自不同
  • 動態載入資料的捕獲

    • 通過requests模組進行資料爬取無法每次都實現可見即可得

    • 有些資料是通過非瀏覽器位址列中的url請求到的資料 而是其他請求請求到的資料,這些通過其他請求請求到的資料就是動態載入的資料。

    • 通過抓包工具進行區域性搜尋,檢測網頁中是否有動態載入資料
      在頁面開啟抓包工具 捕獲到位址列對應資料包 在該資料包的response選項卡搜尋資料 如果沒有就是動態載入的。

    • 捕獲動態載入資料
      定位到動態載入資料對應的資料包,從該資料包中就可以提取出資料
      - 請求url
      - 請求方式
      - 請求攜帶的引數
      - 看到響應資料

    • 基於抓包工具進行全域性搜尋不一定可以每次都能定位到動態載入資料對應的資料包
      如果動態載入的資料是經過加密的密文資料,就抓取不到

                      # 在錄入關鍵字的本文框中錄入搜尋按鈕 發起ajax請求
                      # 當前頁面刷新出來的位置資訊一定是通過ajax請求請求到的資料
                      # 基於抓包工具定位到該ajax請求的請求包 從該資料包中捕獲到
                        # 1.請求的UrL
                        # 2.請求方式
                        # 3.請求攜帶的引數
                        # 4.看到的相應資料
                    
                     # post請求方式 注意data引數是post方法中處理引數動態化的引數
                     # 這裡還有兩個概念 抓xhr包和要用表單資料打包data         
                     response = requests.post(url=url, headers=headers, data=data)
                     page_text = response.json()
      
  • 補充資料:User-Agent彙總

    • safari 5.1 – MAC
      User-Agent:Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50
    • safari 5.1 – Windows
      User-Agent:Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50
    • IE 9.0
      User-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;
    • IE 8.0
      User-Agent:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)
    • IE 7.0
      User-Agent:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)
    • IE 6.0
      User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
    • Firefox 4.0.1 – MAC
      User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
    • Firefox 4.0.1 – Windows
      User-Agent:Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
    • Opera 11.11 – MAC
      User-Agent:Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11
    • Opera 11.11 – Windows
      User-Agent:Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11
    • Chrome 17.0 – MAC
      User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11
    • 傲遊(Maxthon)
      User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)
    • 騰訊TT
      User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; TencentTraveler 4.0)
    • 世界之窗(The World) 2.x
      User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)
    • 世界之窗(The World) 3.x
      User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; The World)
    • 搜狗瀏覽器 1.x
      User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SE 2.X MetaSr 1.0; SE 2.X MetaSr 1.0; .NET CLR 2.0.50727; SE 2.X MetaSr 1.0)
    • 360瀏覽器
      User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)
    • Avant
      User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Avant Browser)
    • Green Browser
      User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)