python 爬虫保存文件的几种方法
Posted 萧白白
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 爬虫保存文件的几种方法相关的知识,希望对你有一定的参考价值。
import os os.makedirs(‘./img/‘, exist_ok=True) IMAGE_URL = "https://morvanzhou.github.io/static/img/description/learning_step_flowchart.png" def urllib_download(): from urllib.request import urlretrieve urlretrieve(IMAGE_URL, ‘./img/image1.png‘) # whole document def request_download(): import requests r = requests.get(IMAGE_URL) with open(‘./img/image2.png‘, ‘wb‘) as f: f.write(r.content) # whole document def chunk_download(): import requests r = requests.get(IMAGE_URL, stream=True) # stream loading with open(‘./img/image3.png‘, ‘wb‘) as f: for chunk in r.iter_content(chunk_size=32): f.write(chunk) urllib_download() print(‘download image1‘) request_download() print(‘download image2‘) chunk_download() print(‘download image3‘)
以上是关于python 爬虫保存文件的几种方法的主要内容,如果未能解决你的问题,请参考以下文章