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

新猿木子李: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')

```

标签:python,bing,新猿,jpg,url,木子李,download,com
来源: https://blog.csdn.net/pythonjiagoushi/article/details/121424465