zoukankan      html  css  js  c++  java
  • Requests方法 -- post

    >>> import requests  导入requests库

    >>> help(requests)  #查看requests方法
    Help on package requests:

    NAME
    requests

    DESCRIPTION
    Requests HTTP Library
    ~~~~~~~~~~~~~~~~~~~~~

    Requests is an HTTP library, written in Python, for human beings. Basic GET
    usage:

    >>> import requests
    >>> r = requests.get('https://www.python.org')
    >>> r.status_code
    200
    >>> 'Python is a programming language' in r.content
    True

    ... or POST:

    >>> payload = dict(key1='value1', key2='value2')
    >>> r = requests.post('https://httpbin.org/post', data=payload)
    >>> print(r.text)
    {
    ...
    "form": {
    "key2": "value2",
    "key1": "value1"
    },
    ...
    }

    The other HTTP methods are supported - see `requests.api`. Full documentation
    is at <http://python-requests.org>.

    :copyright: (c) 2017 by Kenneth Reitz.
    :license: Apache 2.0, see LICENSE for more details.

    做post实例之字典序列化操作:

    >>> import requests
    >>> import json
    >>> payload = {"yy":"1123","gg":"4145"}
    >>> data_json = json.dumps(payload)  # 字典序列化转化用dumps
    >>> r = requests.post("https://httpbin.org/post",json=data_json)
    >>> print(r.text)
    {
    "args": {},
    "data": ""{\"yy\": \"1123\", \"gg\": \"4145\"}"",
    "files": {},
    "form": {},
    "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Content-Length": "38",
    "Content-Type": "application/json",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.21.0"
    },
    "json": "{"yy": "1123", "gg": "4145"}",
    "origin": "223.104.1.157, 223.104.1.157",
    "url": "https://httpbin.org/post"
    }

    如下为字典:  ---- >也可以写成  >>> payload =dict(yy="1123",gg="4145") 的格式

    >>> payload = {"yy":"1123","gg":"4145"}
    >>> r = requests.post("https://httpbin.org/post",data=payload)
    >>> print(r.text)
    {
    "args": {},
    "data": "",
    "files": {},
    "form": {
    "gg": "4145",
    "yy": "1123"
    },
    "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Content-Length": "15",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.21.0"
    },
    "json": null,
    "origin": "223.104.1.157, 223.104.1.157",
    "url": "https://httpbin.org/post"
    }

    如查看某个值: 

    >>> print(r.json()["form"]["gg"])
    4145

  • 相关阅读:
    react native( rn) 中关于navigationOptions中headerRight 获取navigation的问题 rn
    string与number转换
    基础面试资料
    vim快捷键大全
    Vim中的自定义快捷键
    去掉ubuntu命令行模式提示声
    模板非类型形参的详细阐述
    C++中为什么构造函数不能定义为虚函数
    C++ 深入理解 虚继承、多重继承和直接继承
    C++ 虚函数表解析
  • 原文地址:https://www.cnblogs.com/Teachertao/p/11141230.html
Copyright © 2011-2022 走看看