爬虫(4) - Requests(3) | Requests常用场景方法

文件、图片读写

方式一

import requests

r=requests.get("https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/logo_white-d0c9fe2af5.png")
with open('6789.png','wb') as f:
    for chunk in r.iter_content(100000):
         f.write(r.content)

iter_content ( chunk_size = 1,decode_unicode = False )

迭代响应数据。当在请求上设置stream = True时,这避免了立即将内容读入内存以获得大响应。块大小是它应该读入内存的字节数。这不一定是因为可以进行解码而返回的每个项目的长度。

chunk_size必须是int或None类型。值None将根据流的值而有所不同。 stream = True将在接收到块的任何大小时读取数据。如果stream = False,则数据作为单个块返回。

如果decode_unicode为True,将根据响应使用最佳可用编码对内容进行解码。上述代码,它将每次以100000字节迭代响应

方式二

import requests

r=requests.get("https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/logo_white-d0c9fe2af5.png")
with open('6789.png','wb') as f:
      f.write(r.content)

 

 

posted @ 2022-07-01 08:56  葛老头  阅读(79)  评论(0编辑  收藏  举报