新猿木子李:0基础学python培训教程 python下载文件的多种方法汇总

## python 下载文件的多种方法汇总

#### **Requests**

```python

import requests

# 图片来自bing.com

url = 'https://cn.bing.com/th?id=OHR.DerwentIsle_EN-CN8738104578_400x240.jpg'

def requests_download():

  content = requests.get(url).content

  with open('pic_requests.jpg', 'wb') as file:

    file.write(content)

```

#### **urllib**

```python

from urllib import request

# 图片来自bing.com

url = 'https://cn.bing.com/th?id=OHR.DerwentIsle_EN-CN8738104578_400x240.jpg'

def urllib_download():

  request.urlretrieve(url, 'pic_urllib.jpg')

```

**urllib3**

```python

def urllib3_download():

  # 创建一个连接池

  poolManager = urllib3.PoolManager()

  resp = poolManager.request('GET', url)

  with open("pic_urllib3.jpg", "wb") as file:

    file.write(resp.data)

  resp.release_conn()

```

#### **wget**

```python

import wget

# 图片来自bing.com

url = 'https://cn.bing.com/th?id=OHR.DerwentIsle_EN-CN8738104578_400x240.jpg'

def wget_download():

  wget.download(url, out='pic_wget.jpg')

```

上一篇:VxWorks6.6 pcPentium BSP 使用说明(二):创建启动盘


下一篇:常用软件官方下载地址