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

  • 相关阅读:
    WinForm 自定义控件 学习笔记三
    FtpWebRequest相关
    delegate学习笔记1
    C#操作XML
    WinForm 非客户区相关
    WinForm 自定义控件 学习笔记二
    POJ1061 青蛙的约会(扩展欧几里得)
    aaaaaaa……aaa(n个)%p的值 (矩阵快速幂)
    POJ3735 Training little cats(矩阵快速幂)
    数据库日志文件太大的解决方法及原理
  • 原文地址:https://www.cnblogs.com/Teachertao/p/11141230.html
Copyright © 2011-2022 走看看