requests库之处理响应
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                requests库之处理响应
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                response對象:
?
http狀態(tài)碼
利用response把流存儲成圖片或者文件
一個下載圖片的實例
import requestsdef download_pic():#headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.109 Safari/537.36'}url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1502704492717&di=8fa0fd715e093c769cd55ec3043d7c2d&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F01578458056c9ea84a0d304f7fe50f.jpg"response = requests.get(url,stream=True)from contextlib import closingwith closing(requests.get(url,stream=True)) as response:with open('demo.jpg','wb') as fd:for chunk in response.iter_content(128):fd.write(chunk)print(response.status_code)print(response.headers)#print(response.content) download_pic()?
事件鉤子:事件驅(qū)動型開發(fā),基于回調(diào)。
import requestsdef get_key_info(response,*args,**kwargs):print(response.headers['Content-Type'])def main():requests.get('https://www.baidu.com',hooks=dict(response=get_key_info))main()?
轉(zhuǎn)載于:https://www.cnblogs.com/HJhj/p/7354562.html
總結(jié)
以上是生活随笔為你收集整理的requests库之处理响应的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: ASCll码 表
- 下一篇: Windows server 2012
