除了使用官方标准库的urllib,我们可以使用更好用的第三方模块,如requests
Requests 完全满足如今网络的需求,其功能有以下:
国际化域名和 URLs
Keep-Alive & 连接池
持久的 Cookie 会话
类浏览器式的 SSL 加密认证
基本/摘要式的身份认证
优雅的键/值 Cookies
自动解压
Unicode 编码的响应体
多段文件上传
连接超时
支持 .netrc
适用于 Python 2.6—3.4
线程安全
官方文档传送门:HTTP for Humans
http://www.python-requests.org/en/master/
示例:
import requests
request = requests.get("http://weibo.com/", auth = ('user', 'aaa'))
print(request.status_code)
print(request.headers['content-type'])
print(request.encoding)