處理響應

響應基本API

http狀態碼

Response對象
>>dir(response) #查詢response對象API
* statue_code   #狀態碼
* reason    #狀態碼含義
* url
* history #`Response <Response>`list
* elpased #發送request與接收response之間的時間間隔
* request
* encoding 
* raw
#: File-like object representation of response (for advanced usage).
#: Use of ``raw`` requires that ``stream=True`` be set on the request.
# This requirement does not apply for use internally to Requests.
* content
* text
* json
* response.json()['str']    #字典操做提取
複製代碼

下載圖片

  • 利用爬蟲自動下載圖片
  • 遠程下載服務器上的文本文件

圖片下載

瀏覽器模擬:利用Chrome開發者工具獲取瀏覽器的User-Agent 構建request 讀取流data 存入數據git

def img_download():
    url = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_' \
          '10000&sec=1555837805429&di=4daaef4aa7a422aa0da2b5c7a138c988&imgtype=0&src=' \
          'http%3A%2F%2Fcdn.ifanr.cn%2Fwp-content%2Fuploads%2F2014%2F06%2Fgithub.png'
    header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36'}
    response = requests.get(url, headers=header, stream=True)
    print(response.status_code)
    print(response.reason)
    print(response.headers)
    with open('demo.jpg','wb') as fd:
        for chunk in response.iter_content(128):
            fd.write(chunk)

def download_image_improve():
    url = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_' \
          '10000&sec=1555837805429&di=4daaef4aa7a422aa0da2b5c7a138c988&imgtype=0&src=' \
          'http%3A%2F%2Fcdn.ifanr.cn%2Fwp-content%2Fuploads%2F2014%2F06%2Fgithub.png'
    header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36'}
    response = requests.get(url, headers=header, stream=True)
    from contextlib import closing
    with closing(requests.get(url, headers=header, stream=True)) as response:
        with open('demo1.jpg', 'wb') as fd:
            #每128寫入一次
            for chunk in response.iter_content(128):
                fd.write(chunk)
複製代碼

事件鉤子(Evetnt Hooks)

*args 用來將參數打包成tuple給函數體調用 **kwargs 打包關鍵字參數成dict給函數體調用 注意點:參數arg、*args、**kwargs三個參數的位置必須是必定的。必須是(arg,*args,**kwargs)這個順序,不然程序會報錯。github

def get_key_info(response, *args, **kwargs):
    #回調函數
    print(response.headers['Content-Type'])

def main():
    #request請求時註冊回調函數
    requests.get('https://api.github.com', hooks=dict(response=get_key_info))
複製代碼
相關文章
相關標籤/搜索