码迷,mamicode.com
首页 > 编程语言 > 详细

python 爬虫保存文件的几种方法

时间:2018-12-03 21:43:48      阅读:315      评论:0      收藏:0      [点我收藏+]

标签:learning   def   img   script   with   爬虫   import   pen   nload   

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 爬虫保存文件的几种方法

标签:learning   def   img   script   with   爬虫   import   pen   nload   

原文地址:https://www.cnblogs.com/jianglijian/p/10060947.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!