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/
  • 相关阅读:
    C#呓语
    引起超时的原因及表解锁的方法<转>
    如何使用数据库引擎优化顾问优化数据库 <转>
    缩短IIS应用池回收时间,减少IIS假死<转>
    Microsoft Silverlight 4 Tools for Visual Studio 2010中文版本
    系统统一验证(IHttpHandlerFactory)<转>
    解决CSS BUG的顺口溜<转>
    重建索引提高SQL Server性能<转>
    .NET调用osql.exe执行sql脚本创建表和存储过程<转>
    SQL SERVER性能优化综述<转>
  • 原文地址:https://www.cnblogs.com/qianjinyan/p/11435380.html
Copyright © 2011-2022 走看看