码迷,mamicode.com
首页 > 编程语言 > 详细

python Request模块

时间:2019-04-29 15:36:57      阅读:732      评论:0      收藏:0      [点我收藏+]

标签:ica   orm   inf   efault   lex   str   数据   ldb   cli   

---恢复内容开始---

技术图片

Request的五种请求方式:

  • request.get()
  • request.post()
  • request.head()
  • requst.put()
  • request.patch()
  • request.options()

一:request.get():

源码:

技术图片
 1 def get(url, params=None, **kwargs):
 2     r"""Sends a GET request.
 3 
 4     :param url: URL for the new :class:`Request` object.
 5     :param params: (optional) Dictionary, list of tuples or bytes to send
 6         in the body of the :class:`Request`.
 7     :param \*\*kwargs: Optional arguments that ``request`` takes.
 8     :return: :class:`Response <Response>` object
 9     :rtype: requests.Response
10     """
11 
12     kwargs.setdefault(‘allow_redirects‘, True)
13     return request(‘get‘, url, params=params, **kwargs)
requst.get()源码

参数解析及使用

技术图片
1 import requests
2 r=requests.request(‘post‘,‘http://www.oldboyedu.com‘,params={‘k1‘:‘python‘})
3 print(r.url)
4 #https://www.oldboyedu.com/?k1=python
get参数使用

二:request.post()

源码

技术图片
 1 def post(url, data=None, json=None, **kwargs):
 2     r"""Sends a POST request.
 3 
 4     :param url: URL for the new :class:`Request` object.
 5     :param data: (optional) Dictionary, list of tuples, bytes, or file-like
 6         object to send in the body of the :class:`Request`.
 7     :param json: (optional) json data to send in the body of the :class:`Request`.
 8     :param \*\*kwargs: Optional arguments that ``request`` takes.
 9     :return: :class:`Response <Response>` object
10 
11     :rtype: requests.Response
12     """
13     return request(‘post‘, url, data=data, json=json, **kwargs)
request.post()源码

参数解析及使用

技术图片
 1 b=requests.request(‘post‘,‘http://www.oldboyedu.com‘,data={"k1":‘python‘},json={‘k2‘:"linux"})
 2 print(b.url)
 3 print(b.headers)
 4 print(b.json())
 5 #由于post提交参数时,不会在url后面加入参数值,所以时看不出效果的
 6 
 7 
 8 - data:    在请求体里传递的数据
 9             
10                 requests.request(
11                     method=‘POST‘,
12                     url= ‘http://www.oldboyedu.com‘,
13                     params = {‘k1‘:‘v1‘,‘k2‘:‘v2‘},
14                     data = {‘use‘:‘alex‘,‘pwd‘: ‘123‘,‘x‘:[11,2,3]}
15                 )
16                 
17                 请求头:
18                     content-type: application/url-form-encod.....
19                     
20                参数
21                     use=alex&pwd=123
22                 
23 - json   在请求体里传递的数据
24                 requests.request(
25                     method=‘POST‘,
26                     url= ‘http://www.oldboyedu.com‘,
27                     params = {‘k1‘:‘v1‘,‘k2‘:‘v2‘},
28                     json = {‘use‘:‘alex‘,‘pwd‘: ‘123‘}
29                 )
30                 请求头:
31                     content-type: application/json
32                     
33                 参数:
34                     "{‘use‘:‘alex‘,‘pwd‘: ‘123‘}"
post参数

header参数

技术图片
 1 requests.request(
 2                     method=‘POST‘,
 3                     url= ‘http://www.oldboyedu.com‘,
 4                     params = {‘k1‘:‘v1‘,‘k2‘:‘v2‘},
 5                     json = {‘use‘:‘alex‘,‘pwd‘: ‘123‘},
 6                     headers={
 7                         ‘Referer‘: ‘http://dig.chouti.com/‘,
 8                         ‘User-Agent‘: "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"
 9                     }
10                 )
View Code

 

 


---恢复内容结束---

python Request模块

标签:ica   orm   inf   efault   lex   str   数据   ldb   cli   

原文地址:https://www.cnblogs.com/Mr-l/p/10790420.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!