1 简介
requests是python语言编写的HTTP库,底层实现就是urllib
Requests能够自动帮助我们解压(gzip压缩的等)网页内容
2 HTTP协议对资源的操作
GET:请求获取URL位置的资源
POST:请求向URL位置的资源后附加新的数据
PUT:请求向URL位置存储一个资源,覆盖原URL位置的资源
DELETE:请求删除URL位置存储的资源
HEAD:请求获取URL位置资源的响应消息报告,即获得该资源的头部信息
PATCH:请求局部更新URL位置的资源,即改变该处资源的部分内容
3 Request请求
对象
Request:(略)
Response:包含爬虫返回的内容
Request方法
requests.request():构造一个请求,支撑以下各方法的基础方法
requests.get():获取HTML网页的主要方法,对应于HTTP的GET(下载)
requests.head():获取HTML网页头信息的方法,对应于HTTP的HEAD
requests.post():向HTML网页提交POST请求的方法,对应于HTTP的POST(上传)
requests.put():向HTML网页提交PUT请求的方法,对应于HTTP的PUT(重写)
requests.patch():向HTML网页提交局部修改请求,对应于HTTP的PATCH(修改、追加)
requests.delete():向HTML页面提交删除请求,对应于HTTP的DELETE(删除)
Response属性
r.status_code:HTTP请求的返回状态,200表示连接成功,404表示失败
r.text:HTTP响应内容的字符串形式,即,url对应的页面内容
r.encoding:从HTTP header中猜测的响应内容编码方式(header中不存在charset,则认为编码为ISO‐8859‐1,此时需要修改编码方式)
r.apparent_encoding:从内容中分析出的响应内容编码方式(备选编码方式)
r.content:HTTP响应内容的二进制形式
text和content的区别:
requests对象的get和post方法返回网页部分会存在.content和.text两个对象中。两者区别在于,content中间存的是字节码,而text中存的是Beautifulsoup根据猜测的编码方式将content内容编码成字符串。直接输出content,会发现前面存在b'这样的标志,这是字节字符串的标志,而text是',没有前面的b,对于纯ascii码,这两个可以说一模一样,对于其他的文字,需要正确编码才能正常显示。大部分情况建议使用.text,因为显示的是汉字,但有时会显示乱码,这时需要用.content.decode('utf-8'),中文常用utf-8和GBK,GB2312等。这样可以手工选择文字编码方式。所以简而言之,.text是现成的字符串,.content还要编码
4 方法及参数解析
requests.request
requests.request(method, url, **kwargs)
method : 请求方式,对应get/put/post等7种
url : 拟获取页面的url链接
**kwargs: 控制访问的参数,共13个
params: 字典或字节序列,作为参数增加到url中
data: 字典、字节序列或文件对象,作为Request的内容
Json:JSON格式的数据,作为Request的内容
headers:字典,HTTP定制头
cookies:字典或CookieJar,Request中的cookie
auth:元组,支持HTTP认证功能
files:字典类型,传输文件
timeout:设定超时时间,秒为单位
proxies:字典类型,设定访问代理服务器,可以增加登录认证
allow_redirects : True/False,默认为True,重定向开关
stream:True/False,默认为True,获取内容立即下载开关
verify:True/False,默认为True,认证SSL证书开关
cert:本地SSL证书路径
requests.get
requests.get(url, params=None, **kwargs)
url : 拟获取页面的url链接
params : url中的额外参数,字典或字节流格式,可选
**kwargs: 12个控制访问的参数
html解析
import requests
r = requests.get('https://www.baidu.com/')
print(r.apparent_encoding) ## 获取父类的编码
print(r.encoding) ## 获取的编码
print(r.status_code) ##返回状态
r.encoding = 'utf-8' ## 设置字体格式
print(type(r)) ## 获取返回信息内容
print(r.text[0:100]) ## 获取文本格式的文件
print('-------------------------')
print(r.content[0:40]) ## 获取二进制格式的文件,一般用于图片
print('-------------------------')
print(r.headers)
>utf-8
>ISO-8859-1
>200
><class 'requests.models.Response'>
<!DOCTYPE html>
<!-...(截取)
-------------------------
>b'<!DOCTYPE html>\r\n<!--STATUS OK--><html> '...(截取)
-------------------------
>{'Cache-Control': 'private, no-cache, no-store, proxy-revalidate, no-transform', 'Connection': 'Keep-Alive', 'Content-Encoding': 'gzip', 'Content-Type': 'text/html'...(截取)
url参数化
import requests
kv = {'key1': 'value1', 'key2': 'value2'}
r = requests.request('GET', 'http://python123.io/ws', params=kv)
print(r.url)
Post方法
import requests
payload = {'key1':'value1', 'key2': 'value2'}
r = requests.request('post','http://httpbin.org/post', data = payload)
print(r.text)
## 输出
{"args":{},"data":"","files":{},"form":{"key1":"value1","key2":"value2"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Content-Length":"23","Content-Type":"application/x-www-form-urlencoded","Host":"httpbin.org","User-Agent":"python-requests/2.19.1"},"json":null,"origin":"171.113.43.122","url":"http://httpbin.org/post"}
5 Request库的异常
requests.ConnectionError:网络连接错误异常,如DNS查询失败、拒绝连接等
requests.HTTPError:HTTP错误异常
requests.URLRequired:URL缺失异常
requests.TooManyRedirects:超过最大重定向次数,产生重定向异常
requests.ConnectTimeout:连接远程服务器超时异常
requests.Timeout:请求URL超时,产生超时异常
r.raise_for_status():方法内部判断如果不是200,产生异常 requests.HTTPError
6 代码
通用代码框架
import requests
def getHTMLText(url):
try:
r = requests.get(url,timeout = 10)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except:
return '产生异常'
if __name__ == '__main__':
url = 'http://www.baidu.com'
print(getHTMLText(url))
百度搜索
import requests
def baidu(keyword):
try:
kv = {'wd':keyword}
hv = {'user-agent': 'Mozilla/5.0'}
url = 'http://www.baidu.com/s'
r = requests.get(url,params = kv,headers = hv)
r.raise_for_status()
r.encoding = r.apparent_encoding
print(r.text[-10:])
print(len(r.text))
print(r.request.url)
except:
print('爬取失败')
baidu('python')
>0223915-->
219241
http://www.baidu.com/s?wd=python
下载图片
import requests
import os
url = 'http://img0.dili360.com/rw17/ga/M02/49/B7/wKgBzFqo8ySAT4nUAAry7yQ0MW4188.tub.jpg'
root = 'C:/Users/ZY/Desktop/'
path = root + url.split('/')[-1] ## 路径:原路径+url最后一个'/'之后的部分
try:
if not os.path.exists(root):
os.mkdir(root)
if not os.path.exists(path):
r = requests.get(url)
with open(path,'wb') as f:
f.write(r.content)
f.close()
print('文件保存成功')
else:
print('文件已存在')
except:
print('爬取失败')
IP查询
import requests
url = 'http://m.ip138.com/ip.asp?ip='
ip = '192.18.1.23'
try:
r = requests.get(url + ip)
r.raise_for_status()
r.encoding = r.apparent_encoding
print(r.status_code)
except:
print('爬取失败')