python3.6的request

本文通过三个实例展示了如何利用Python的Requests库进行GET和POST请求,并设置cookies。每个实例都详细介绍了请求的过程及返回结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值