request的基本应用

一、安装

  pip install requests (mac前面加sudo)

二、requests的一些参数

method:一般是用的那种请求方法,是get还是post,delete或者delete

url:是发送的请求地址

params:请求参数,针对get的

json:一般是针对post来说的

headers:请求头

cookies:带着cookies去发送请求,一般是登陆获取到

timeout:最大的超时时间

三、requests的一些方法

 get方法,以百度为例

(放在get上面,按住control+鼠标左键)进入方法内部查看

看r有哪些方法:

['__attrs__', '__bool__', '__class__', '__delattr__', '__dict__', '__doc__', '__enter__', '__exit__', '__format__',
'__getattribute__', '__getstate__', '__hash__', '__init__', '__iter__', '__module__', '__new__', '__nonzero__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__',
'__weakref__', '_content', '_content_consumed', '_next',
'apparent_encoding', 'close', 'connection', 'content', 'cookies', 'elapsed',
'encoding', 'headers', 'history', 'is_permanent_redirect', 'is_redirect',
'iter_content', 'iter_lines', 'json', 'links', 'next', 'ok', 'raise_for_status',
'raw', 'reason', 'request', 'status_code', 'text', 'url']

我们要看的几个重要的就是 

text(文本信息),status_code(状态码),cookies,headers,url(请求地址)等

但是不可以用r.json()这个方法输出,原因是,百度首页显示的格式是html,所以没有json字符串,所以会报一个错误,json.decoder.JSONDecodeError

 

猜你喜欢

转载自www.cnblogs.com/peiminer/p/9317555.html