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

python3.6的request

时间:2018-04-09 15:05:41      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:python3   coding   实例   status   .post   head   cep   rgs   nbsp   

request实例1:


import requests payload = {‘key1‘:‘value‘,‘key2‘:‘value2‘} url = "http://httpbin.org/get" headers = {‘content-type‘: ‘application/json‘} res = requests.get(url,params=payload,headers=headers) res.encoding="utf-8" print("1.url: ") print(res.url) print("2.text: ") print(res.text) print("3.json: ") print(res.json()) print("4.status_code: ") print(res.status_code)

结果:

1.url: 
http://httpbin.org/get?key1=value&key2=value2
2.text: 
{
  "args": {
    "key1": "value", 
    "key2": "value2"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Content-Type": "application/json", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.18.4"
  }, 
  "origin": "117.25.182.2", 
  "url": "http://httpbin.org/get?key1=value&key2=value2"
}

3.json: 
{‘args‘: {‘key1‘: ‘value‘, ‘key2‘: ‘value2‘}, ‘headers‘: {‘Accept‘: ‘*/*‘, ‘Accept-Encoding‘: ‘gzip, deflate‘, ‘Connection‘: ‘close‘, ‘Content-Type‘: ‘application/json‘, ‘Host‘: ‘httpbin.org‘, ‘User-Agent‘: ‘python-requests/2.18.4‘}, ‘origin‘: ‘117.25.182.2‘, ‘url‘: ‘http://httpbin.org/get?key1=value&key2=value2‘}
4.status_code: 
200

request实例2:

import requests
payload = {‘key1‘:‘value1‘,‘key2‘:‘value2‘}
url = "http://httpbin.org/post"
headers = {‘content-type‘: ‘application/json‘}
res =requests.post(url,data=payload,headers=headers)
print("1.url: ")
print(res.url)
print("2.text: ")
print(res.text)
print("3.json: ")
print(res.json())
print("4.status_code: ")
print(res.status_code)

  结果:

 

1.url: 
http://httpbin.org/post
2.text: 
{
  "args": {}, 
  "data": "key1=value1&key2=value2", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Content-Length": "23", 
    "Content-Type": "application/json", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.18.4"
  }, 
  "json": null, 
  "origin": "117.25.182.2", 
  "url": "http://httpbin.org/post"
}

3.json: 
{‘args‘: {}, ‘data‘: ‘key1=value1&key2=value2‘, ‘files‘: {}, ‘form‘: {}, ‘headers‘: {‘Accept‘: ‘*/*‘, ‘Accept-Encoding‘: ‘gzip, deflate‘, ‘Connection‘: ‘close‘, ‘Content-Length‘: ‘23‘, ‘Content-Type‘: ‘application/json‘, ‘Host‘: ‘httpbin.org‘, ‘User-Agent‘: ‘python-requests/2.18.4‘}, ‘json‘: None, ‘origin‘: ‘117.25.182.2‘, ‘url‘: ‘http://httpbin.org/post‘}
4.status_code: 
200

 request实例3: 

import requests
url = ‘http://httpbin.org/cookies‘
cookies = dict(cookies_are=‘working‘)
r = requests.get(url, cookies=cookies) 
print("1.url: ")
print(res.url)
print("2.text: ")
print(res.text)
print("3.json: ")
print(res.json())
print("4.status_code: ")
print(res.status_code)

  

结果:

1.url: 
http://httpbin.org/post
2.text: 
{
  "args": {}, 
  "data": "key1=value1&key2=value2", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Content-Length": "23", 
    "Content-Type": "application/json", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.18.4"
  }, 
  "json": null, 
  "origin": "117.25.182.2", 
  "url": "http://httpbin.org/post"
}

3.json: 
{‘args‘: {}, ‘data‘: ‘key1=value1&key2=value2‘, ‘files‘: {}, ‘form‘: {}, ‘headers‘: {‘Accept‘: ‘*/*‘, ‘Accept-Encoding‘: ‘gzip, deflate‘, ‘Connection‘: ‘close‘, ‘Content-Length‘: ‘23‘, ‘Content-Type‘: ‘application/json‘, ‘Host‘: ‘httpbin.org‘, ‘User-Agent‘: ‘python-requests/2.18.4‘}, ‘json‘: None, ‘origin‘: ‘117.25.182.2‘, ‘url‘: ‘http://httpbin.org/post‘}
4.status_code: 
200

  

 

python3.6的request

标签:python3   coding   实例   status   .post   head   cep   rgs   nbsp   

原文地址:https://www.cnblogs.com/amoyzhu/p/8759208.html

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