网络爬虫(五)

上一节说完了GET的方式了,这一节说一下POSt

import requests
data = {'name': 'zhou',
        'age': '22',
}
r1 = requests.post('http://httpbin.org/post', data=data)
print(r1.text)

依旧简单是不是,但是注意:GET方式传递参数的时候是params,而POST方式是data

结果如下:

{"args":{},"data":"","files":{},"form":{"age":"22","name":"zhou"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Content-Length":"16","Content-Type":"application/x-www-form-urlencoded","Host":"httpbin.org","User-Agent":"python-requests/2.18.4"},"json":null,"origin":"218.82.9.110","url":"http://httpbin.org/post"}

其中的form就是咱们提交的数据:

响应:

发送请求之后得到的自然就是响应。我们上几节用了text和content方法,当然还有其他的方法,比如说状态码,响应头,Cookies等

# -*- coding:UTF-8 -*-
__autor__ = 'zhouli'
__date__ = '2018/7/4 23:07'
import requests
import re
r = requests.get("http://jianshu.com")
print(type(r.status_code), r.status_code)  # 打印状态码
print(type(r.headers), r.headers)  # 响应头
print(type(r.cookies), r.cookies)  # cookies
print(type(r.url), r.url)  # 打印URL
print(type(r.history), r.history)  # 输出history属性得到的请求历史

得到的结果如下:

<class 'int'> 403
<class 'requests.structures.CaseInsensitiveDict'> {'Date': 'Wed, 04 Jul 2018 15:47:58 GMT', 'Server': 'Tengine', 'Content-Type': 'text/html', 'Transfer-Encoding': 'chunked', 'Strict-Transport-Security': 'max-age=31536000; includeSubDomains; preload', 'Content-Encoding': 'gzip', 'X-Via': '1.1 PSfjfzdx2mj93:9 (Cdn Cache Server V2.0), 1.1 houdianxinxiazai63:10 (Cdn Cache Server V2.0)', 'Connection': 'keep-alive', 'X-Dscp-Value': '0'}
<class 'requests.cookies.RequestsCookieJar'> <RequestsCookieJar[]>
<class 'str'> https://www.jianshu.com/
<class 'list'> [<Response [301]>, <Response [301]>]

以上就是requests的基本用法。

接下来是高级用法:待续————————————————————————————————————————————————————————————————————

猜你喜欢

转载自www.cnblogs.com/zhoulixiansen/p/9266074.html