日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

requests库之处理响应

發(fā)布時(shí)間:2023/12/20 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 requests库之处理响应 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

response對(duì)象:

?

http狀態(tài)碼

利用response把流存儲(chǔ)成圖片或者文件

一個(gè)下載圖片的實(shí)例

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ū)動(dòng)型開發(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库之处理响应的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。