python3-网络数据下载

1、使用urllib

# import urllib
from urllib import request

print("downloading with urllib")
url = 'https://raw.githubusercontent.com/abidrahmank/OpenCV2-Python-Tutorials/master/data/left.jpg'
print ("downloading with urllib")

# 第一个参数网络地址;第二个参数 本地保存位置
request.urlretrieve(url,"./left2.jpg")

2、使用requests

import requests

print ("downloading with requests")
url = 'https://raw.githubusercontent.com/abidrahmank/OpenCV2-Python-Tutorials/master/data/left.jpg'
with open("./left2.jpg", "wb") as code:
   code.write(requests.get(url).content)

3、使用urllib3

如果没有urllib3,先执行 sudo pip3 install urllib3

import urllib3

print ("downloading with urllib3")
http=urllib3.PoolManager()
url = 'https://raw.githubusercontent.com/abidrahmank/OpenCV2-Python-Tutorials/master/data/left.jpg'
r=http.request('GET',url)

with open("./left2.jpg", "wb") as code:
   code.write(r.data)

4、下载文件并解压读取

# -*- coding: UTF-8 -*-

"""
下载一个压缩包文件,并执行解压
"""
import urllib3
import tarfile
import os
import shutil
import gzip


if not os.path.exists("./t10k-labels-idx1-ubyte.gz"):
   # 下载 文件
   print ("downloading with urllib3")
   http=urllib3.PoolManager()
   url = 'http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz'
   r=http.request('GET',url)

   with open("./t10k-labels-idx1-ubyte.gz", "wb") as code:
      code.write(r.data)

   with gzip.open('./t10k-labels-idx1-ubyte.gz', 'rb') as read, open('t10k-labels-idx1-ubyte', 'wb') as write:
      shutil.copyfileobj(read, write)

else:

   # 读取
   print('直接读取.gz文件')
   with gzip.open('./t10k-labels-idx1-ubyte.gz', 'rb') as read:
      data = read.read()
      print(data)

   # 解压 在读取
   print('先解压在读取')
   with gzip.open('./t10k-labels-idx1-ubyte.gz', 'rb') as read, open('t10k-labels-idx1-ubyte', 'wb') as write:
         shutil.copyfileobj(read, write)

   with open('t10k-labels-idx1-ubyte','rb') as read:
      data = read.read()
      print(data)

也可以使用shell脚本执行下载和解压,在命令终端输入: sh xxx.sh 执行命令

xxx.sh

#!/bin/bash

# 下载文件
wget http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz

# 解压
gunzip  t10k-labels-idx1-ubyte.gz

其他类型的压缩文件使用参考:http://blog.csdn.net/wc781708249/article/details/78192924

5、其他

# -*- coding:utf-8 -*-

'''
实现从网络上下载图片
'''

import urllib.request
import shutil

MODEL_URL='https://image.baidu.com/search/down?tn=download&word=download&ie=' \
          'utf8&fr=detail&url=https%3A%2F%2Ftimgsa.baidu.com%2Ftimg%3Fimage%26quality' \
          '%3D80%26size%3Db9999_10000%26sec%3D1520315813170%26di%3Df0cf0f7c8e041be005d78971c' \
          '5c239d5%26imgtype%3D0%26src%3Dhttp%253A%252F%252Fwww.bapimi.com%252Fuploads%252F0_' \
          '2501250005x2627376053_23.jpg&thumburl=https%3A%2F%2Fss2.bdstatic.com%2F70cFvnSh_' \
          'Q1YnxGkpoWK1HF6hhy%2Fit%2Fu%3D3436138697%2C1578547663%26fm%3D27%26gp%3D0.jpg'
model_path='./5555.jpg'
def download(model_path, verbose=1):
    """
    model_path: 存放的本地路径
    """
    if verbose > 0:
        print("Downloading pretrained model to " + model_path + " ...")
    with urllib.request.urlopen(MODEL_URL) as resp, open(model_path, 'wb') as out:
        shutil.copyfileobj(resp, out)
    if verbose > 0:
        print("... done downloading pretrained model!")

download(model_path)
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值