python用 requests 模块从 Web 下载文件

requests 模块让你很容易从 Web 下载文件,不必担心一些复杂的问题,诸如网络错误、连接问题和数据压缩。requests 模块不是 Python 自带的,所以必须先安装。通过命令行,运行 pip install requests。编写 requests 模块是因为 Python 的 urllib2 模块用起来太复杂。实际上,请拿一支记号笔涂黑这一段。忘记我曾提到 urllib2。如果你需要从 Web 下载东西,使用requests 模块就好了。接下来,做一个简单的测试,确保 requests 模块已经正确安装。在交互式环境中输入以下代码:

>>> import requests

如果没有错误信息显示,requests 模块就已经安装成功了。

用 requests.get()函数下载一个网页

requests.get()函数接受一个要下载的 URL 字符串。通过在 requests.get()的返回值上调用 type(),你可以看到它返回一个 Response 对象,其中包含了 Web 服务器对你的请求做出的响应。稍后我将更详细地解释 Response 对象,但现在请在交互式环境中输入以下代码,并保持计算机与因特网的连接:

>>> import requests
>>> res = requests.get('http://www.gutenberg.org/cache/epub/1112/pg1112.txt')
>>> type(res)
<class 'requests.models.Response'>
>>> res.status_code == requests.codes.ok
True
>>> len(res.text)
178981
>>> print(res.text[:250])
The Project Gutenberg EBook of Romeo and Juliet, by William Shakespeare
This eBook is for the use of anyone anywhere at no cost and with
almost no restrictions whatsoever. You may copy it, give it away or
re-use it under the terms of the Proje

该 URL 指向一个文本页面,其中包含整部罗密欧与朱丽叶,它是由古登堡计划提供的。通过检查 Response 对象的 status_code 属性,你可以了解对这个网页的请求是否成功。如果该值等于requests.codes.ok,那么一切都好(顺便说一下,HTTP协议中“OK”的状态码是 200。你可能已经熟悉404 状态码,它表示“没找到”)。如果请求成功,下载的页面就作为一个字符串,保存在 Response 对象的 text变量中。这个变量保存了包含整部戏剧的一个大字符串,调用 len(res.text)表明,它的长度超过 178000 个字符。最后,调用 print(res.text[:250])显示前 250 个字符。

检查错误

正如你看到的,Response 对象有一个 status_code 属性,可以检查它是否等于requests.codes.ok,了解下载是否成功。检查成功有一种简单的方法,就是在 Response对象上调用 raise_for_status()方法。如果下载文件出错,这将抛出异常。如果下载成功,就什么也不做。在交互式环境中输入以下代码:

>>> res = requests.get('http://inventwithpython.com/page_that_does_not_exist')
>>> res.raise_for_status()
Traceback (most recent call last):
    File "<pyshell#138>", line 1, in <module>
        res.raise_for_status()
    File "C:\Python34\lib\site-packages\requests\models.py", line 773, in raise_for_status
        raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 404 Client Error: Not Found

raise_for_status()方法是一种很好的方式,确保程序在下载失败时停止。这是一件好事:你希望程序在发生未预期的错误时,马上停止。如果下载失败对程序来说不够严重,可以用 try 和 except 语句将 raise_for_status()代码行包裹起来,处理这一错误,不让程序崩溃。

import requests
res = requests.get('http://inventwithpython.com/page_that_does_not_exist')
try:
    res.raise_for_status()
except Exception as exc:
    print('There was a problem: %s' % (exc))

这次 raise_for_status()方法调用导致程序输出以下内容:

There was a problem: 404 Client Error: Not Found

总是在调用 requests.get()之后再调用 raise_for_status()。你希望确保下载确实成功,然后再让程序继续。

将下载的文件保存到硬盘

现在,可以用标准的 open()函数和 write()方法,将 Web 页面保存到硬盘中的一个文件。但是,这里稍稍有一点不同。首先,必须用“写二进制”模式打开该文件,即向函数传入字符串'wb',作为 open()的第二参数。即使该页面是纯文本的(例如前面下载的罗密欧与朱丽叶的文本),你也需要写入二进制数据,而不是文本数据,目的是为了保存该文本中的“Unicode 编码”。

你可以通过以下网页了解更多的Unicode相关内容:

• Joel on Software: The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!): http://www.joelonsoftware.com/articles/Unicode.html
• Pragmatic Unicode: http://nedbatchelder.com/text/unipain.html

为了将 Web 页面写入到一个文件,可以使用 for 循环和 Response 对象的iter_content()方法。

>>> import requests
>>> res = requests.get('http://www.gutenberg.org/cache/epub/1112/pg1112.txt')
>>> res.raise_for_status()
>>> playFile = open('RomeoAndJuliet.txt', 'wb')
>>> for chunk in res.iter_content(100000):
...    playFile.write(chunk)

100000
78981
>>> playFile.close()

iter_content()方法在循环的每次迭代中,返回一段内容。每一段都是 bytes 数据类型,你需要指定一段包含多少字节。10 万字节通常是不错的选择,所以将 100000作为参数传递给 iter_content()。文件 RomeoAndJuliet.txt 将存在于当前工作目录。请注意,虽然在网站上文件名是 pg1112.txt,但在你的硬盘上,该文件的名字不同。requests 模块只处理下载网页内容。一旦网页下后,它就只是程序中的数据。即使在下载该网页后断开了因特网连接,该页面的所有数据仍然会在你的计算机中。write()方法返回一个数字,表示写入文件的字节数。在前面的例子中,第一段包含 100000 个字节,文件剩下的部分只需要 78981 个字节。

回顾一下,下载并保存到文件的完整过程如下:
1.调用 requests.get()下载该文件。
2.用'wb'调用 open(),以写二进制的方式打开一个新文件。
3.利用 Respose 对象的 iter_content()方法做循环。
4.在每次迭代中调用 write(),将内容写入该文件。
5.调用 close()关闭该文件。

这就是关于 requests 模块的全部内容!相对于写入文本文件的 open()/write()/close()工作步骤,for 循环和 iter_content()的部分可能看起来比较复杂,但这是为了确保 requests 模块即使在下载巨大的文件时也不会消耗太多内存。你可以访问http://requests.readthedocs.org/,了解 requests 模块的其他功能。

  • 5
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
requestsPython中一个常用的HTTP库,用于向Web服务器发送HTTP/1.1请求。 使用requests模块,可以方便地进行GET、POST等请求,并处理响应结果。 以下是requests模块的基本用法: 1. 安装requests模块:在命令行中输入 pip install requests。 2. 发送GET请求: ```python import requests url = 'http://www.example.com' response = requests.get(url) print(response.text) ``` 3. 发送带参数的GET请求: ```python import requests url = 'http://www.example.com' params = {'key1': 'value1', 'key2': 'value2'} response = requests.get(url, params=params) print(response.text) ``` 4. 发送POST请求: ```python import requests url = 'http://www.example.com' data = {'key1': 'value1', 'key2': 'value2'} response = requests.post(url, data=data) print(response.text) ``` 5. 发送带文件的POST请求: ```python import requests url = 'http://www.example.com' files = {'file': open('example.txt', 'rb')} response = requests.post(url, files=files) print(response.text) ``` 6. 发送带headers的请求: ```python import requests url = 'http://www.example.com' headers = {'User-Agent': 'Mozilla/5.0'} response = requests.get(url, headers=headers) print(response.text) ``` 7. 处理异常: ```python import requests url = 'http://www.example.com' try: response = requests.get(url, timeout=1) response.raise_for_status() except requests.exceptions.RequestException as e: print(e) ``` 以上就是requests模块的基本用法。更多高级用法可以查看官方文档:https://requests.readthedocs.io/en/latest/。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值