zoukankan      html  css  js  c++  java
  • python的request模块快速上手

    1.引入模块:import request

    2.可以使用get、post、put、delete、head、options,使用方法:r = request.get('www.baidu.com')

    3.请求带参数:

        payload = {'key1': 'value1', 'key2': ['value2', 'value3']}

        r = request.get(url,params=payload)

     此时r.url得值为:url?key1=value1&key2=value2

    4.post请求:

     payload = {'key1': 'value1', 'key2': 'value2'}

     r = requests.post(url, data=payload)

    5.定制请求头:

     headers = {'user-agent': 'my-app/0.0.1'}

     r = requests.get(url, headers = headers)

    6.发送和获取cookie:

     cookies = dict(cookies_are='working')

     r = requests.get(url, cookies=cookies)  //发送

     r.cookies['example_cookie_name']  //获取

    7.常用属性和方法:

     r.text : 获取服务器相应得内容

     r.encoding = 'utf-8' : 设置编码方式

     r.json() : json解码

     r.status_code : 检测响应状态码

    初级参考原文:http://docs.python-requests.org/zh_CN/latest/user/quickstart.html

    高级用法请看:http://docs.python-requests.org/zh_CN/latest/user/advanced.html#advanced

     

  • 相关阅读:
    Python自学笔记(12day)
    Python自学笔记(11day)
    Python自学笔记(10day)
    Python自学笔记(9day)
    Python自学笔记(8day)
    form标签的使用
    form标签的使用法
    img标签的使用方法
    <a></a>标签的使用
    html的标签
  • 原文地址:https://www.cnblogs.com/zhenxianluo/p/6401401.html
Copyright © 2011-2022 走看看