1. 程式人生 > >25-1 request模組介紹

25-1 request模組介紹

requests模組

- 基於如下5點展開requests模組的學習

  • 什麼是requests模組
    • requests模組是python中原生的基於網路請求的模組,其主要作用是用來模擬瀏覽器發起請求。功能強大,用法簡潔高效。在爬蟲領域中佔據著半壁江山的地位。
  • 為什麼要使用requests模組
    • 因為在使用urllib模組的時候,會有諸多不便之處,總結如下:
      • 手動處理url編碼
      • 手動處理post請求引數
      • 處理cookie和代理操作繁瑣
      • ......
    • 使用requests模組:
      • 自動處理url編碼
      • 自動處理post請求引數
      • 簡化cookie和代理操作
      • ......
  • 如何使用requests模組
    • 安裝:
      • pip install requests
    • 使用流程
      • 指定url
      • 基於requests模組發起請求
      • 獲取響應物件中的資料值
      • 持久化儲存
  • 通過5個基於requests模組的爬蟲專案對該模組進行學習和鞏固
    • 基於requests模組的get請求
      • 需求:爬取搜狗指定詞條搜尋後的頁面資料
    • 基於requests模組的post請求
      • 需求:登入豆瓣電影,爬取登入成功後的頁面資料
    • 基於requests模組ajax的get請求
    • 基於requests模組ajax的post請求
    • 綜合練習
      • 需求:爬取搜狗知乎指定詞條指定頁碼下的頁面資料

- 程式碼展示

  • 需求:爬取搜狗指定詞條搜尋後的頁面資料
    import requests
    import os
    #指定搜尋關鍵字
    word = input('enter a word you want to search:')
    #自定義請求頭資訊
    headers={
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
        }
    #指定url
    url = 'https://www.sogou.com/web'
    #封裝get請求引數
    prams = {
        'query':word,
        'ie':'utf-8'
    }
    #發起請求
    response = requests.get(url=url,params=param)
    
    #獲取響應資料
    page_text = response.text
    
    with open('./sougou.html','w',encoding='utf-8') as fp:
        fp.write(page_text)

     

     

  • 需求:登入豆瓣電影,爬取登入成功後的頁面資料
    import requests
    import os
    url = 'https://accounts.douban.com/login'
    #封裝請求引數
    data = {
        "source": "movie",
        "redir": "https://movie.douban.com/",
        "form_email": "15027900535",
        "form_password": "[email protected]",
        "login": "登入",
    }
    #自定義請求頭資訊
    headers={
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
        }
    response = requests.post(url=url,data=data)
    page_text = response.text
    
    with open('./douban111.html','w',encoding='utf-8') as fp:
        fp.write(page_text)

     

     

  • 需求:爬取豆瓣電影分類排行榜 https://movie.douban.com/中的電影詳情資料
    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    import requests
    import urllib.request
    if __name__ == "__main__":
    
        #指定ajax-get請求的url(通過抓包進行獲取)
        url = 'https://movie.douban.com/j/chart/top_list?'
    
        #定製請求頭資訊,相關的頭資訊必須封裝在字典結構中
        headers = {
            #定製請求頭中的User-Agent引數,當然也可以定製請求頭中其他的引數
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36',
        }
    
        #定製get請求攜帶的引數(從抓包工具中獲取)
        param = {
            'type':'5',
            'interval_id':'100:90',
            'action':'',
            'start':'0',
            'limit':'20'
        }
        #發起get請求,獲取響應物件
        response = requests.get(url=url,headers=headers,params=param)
    
        #獲取響應內容:響應內容為json串
        print(response.text)

     

     

  • 需求:爬取肯德基餐廳查詢http://www.kfc.com.cn/kfccda/index.aspx中指定地點的餐廳資料
    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    import requests
    import urllib.request
    if __name__ == "__main__":
    
        #指定ajax-post請求的url(通過抓包進行獲取)
        url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword'
    
        #定製請求頭資訊,相關的頭資訊必須封裝在字典結構中
        headers = {
            #定製請求頭中的User-Agent引數,當然也可以定製請求頭中其他的引數
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36',
        }
    
        #定製post請求攜帶的引數(從抓包工具中獲取)
        data = {
            'cname':'',
            'pid':'',
            'keyword':'北京',
            'pageIndex': '1',
            'pageSize': '10'
        }
        #發起post請求,獲取響應物件
        response = requests.get(url=url,headers=headers,data=data)
    
        #獲取響應內容:響應內容為json串
        print(response.text)
     

     

  • 需求:爬取搜狗知乎指定詞條指定頁碼下的頁面資料
    import requests
    import os
    #指定搜尋關鍵字
    word = input('enter a word you want to search:')
    #指定起始頁碼
    start_page = int(input('enter start page num:'))
    end_page = int(input('enter end page num:'))
    #自定義請求頭資訊
    headers={
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
        }
    #指定url
    url = 'https://zhihu.sogou.com/zhihu'
    #建立資料夾
    if not os.path.exists('./sougou'):
        os.mkdir('./sougou')
    for page in range(start_page,end_page+1):
        #封裝get請求引數
        params = {
            'query':word,
            'ie':'utf-8',
            'page':str(page)
        }
        #發起post請求,獲取響應物件
        response = requests.get(url=url,params=params)
        #獲取頁面資料
        page_text = response.text
        fileName = word+'_'+str(page)+'.html'
        filePath = './sougou/'+fileName
        with open(filePath,'w',encoding='utf-8') as fp:
            fp.write(page_text)
            print('爬取'+str(page)+'頁結束')