zoukankan      html  css  js  c++  java
  • Python语言之requests库

    发送请求、传递URL参数、定制headers、接受数据,处理数据等

    在Java中用httpclient jar包,在Python中用requests库,即使没有事先下载,在Pycharm开发工具中,出现提示时刻,同意安装即可

    1) 发送请求

    url='http://baidu.com'
    requests.post(url)
    requests.put(url)
    requests.delete(url)
    requests.head(url)
    requests.options(url)
    

      

    GET: 请求指定的页面信息,并返回实体主体。
    HEAD: 只请求页面的首部。
    POST: 请求服务器接受所指定的文档作为对所标识的URI的新的从属实体。
    PUT: 从客户端向服务器传送的数据取代指定的文档的内容。
    DELETE: 请求服务器删除指定的页面。
    get 和 post比较常见 GET请求将提交的数据放置在HTTP请求协议头中
    POST提交的数据则放在实体数据中

    2) 定制参数

    import requests
    response = requests.get("http://localhost:8083/showuser?name=germey&age=22")
    response = requests.get("http://localhost:8083/showuser?name=germey&age=22")
    
    response = requests.get("http://localhost:8083/showuser?name=germey&age=22")
    data={
     'name': 'germey',
     'age': 22
    }
    response = requests.get("http://localhost:8083/showuser,param=data}
    data2= (
        ('endDate', '2019-12-12'),
        ('pageIndex', 0),
        ('pageSize', 50),
        ('startDate', '2010-01-01')
    )
    
    data3= {
      "endDate": "2019-12-12",
      "pageIndex": 0,
      "pageSize": 50,
      "startDate": "2010-01-01"
    }
    
    
    response2=requests.post(url=url2,headers=headers2,data=json.dumps(data2))
    response2=requests.post(url=url2,headers=headers2,data=json.dumps(data3))
    print(response2.json())
    

      

    3)定制header

    url2='http://localhost:8094/common-api/v1/point/detail?social=AcxiomChina&user=VP巴黎恋人'
    headers2={'token':'MTU1Mjg4NTQxOEFDWElPTS1XRUNIQVQ=','Content-Type':'application/json;charset=UTF-8'}


    4)response的处理 

    response.json() 如果是json格式的返回,转化为json格式
    response.cookies
    response.status_code
    response.url
    response.text
    response.content

    http://cn.python-requests.org/zh_CN/latest/
  • 相关阅读:
    VM启用ISO共享
    部署服务--NLB
    SCVMM问题汇总
    判断文件是否存在(exist)
    函数(Function)作用域 / 远程函数执行
    基于433MHz无线串口,多发一收解决方案
    ZigBee自组网地址分配与路由协议概述
    Zigbee协议栈--Z-Stack的使用
    RT-Thread RTOS
    信息量、互斥信息量和事件标志
  • 原文地址:https://www.cnblogs.com/qianjinyan/p/11435380.html
Copyright © 2011-2022 走看看