zoukankan      html  css  js  c++  java
  • python--requests模块

    import requests

    1、发get请求
    url = 'http://api.nnzhp.cn/api/user/stu_info'
    data = {'stu_name':'小黑'}  #请求数据
    req = requests.get(url,params=data)  #get请求
    print(req.json())  #字典
    print(req.text)    #stringjson

    返回的都是什么
    返回的类型是什么
    中文的好使吗
    2 、发post请求
    url = 'http://api.nnzhp.cn/api/user/login'
    data = {'username':'niuhanyang','passwd':'aA123456'}  #请求数据
    req = requests.post(url,data)  #发送post请求
    print(req.json())

    3、入参是json类型的
    import random
    phone=random.randint(10000000000,99999999999)
    url='http://api.nnzhp.cn/api/user/add_stu'
    data =  {
        "name":"1",
        "grade":"天蝎座",
        "phone":phone,
        "sex":"",
        "age":28,
        "addr":"河南省济源市北海大道32"
      }
    req = requests.post(url,json=data)
    print(req.json())

    4、添加cookie
    url = 'http://api.nnzhp.cn/api/user/gold_add'
    data = {'stu_id':468,'gold':10000}
    djl = {'niuhanyang':'337ca4cc825302b3a8791ac7f9dc4bc6'}
    req = requests.post(url,data,cookies=djl)
    print(req.json())

    5、添加header
    url = 'http://api.nnzhp.cn/api/user/all_stu'
    header = {
        'Referer':'http://api.nnzhp.cn/'
    }
    req = requests.get(url,headers=header)
    print(req.json())

    6、上传文件
    url= 'http://api.nnzhp.cn/api/file/file_upload'
    data = {
        'file':open(r'C:UsersjniuhanyangDesktop6bd9026dt935575932465&690.jpg','rb')
    }
    req= requests.post(url,files=data)
    print(req.json())

    7、下载文件
    url = 'http://up.mcyt.net/?down/46779.mp3'
    req = requests.get(url)
    fw = open('aaa.mp3','wb')
    fw.write(req.content)

  • 相关阅读:
    mysql--------常用命令
    PHP--------微商城实现微信授权登录
    mysql--------命令来操作表
    PHP--------解决网址URL编码问题
    php-----utf8和gbk相互转换
    javascript遍历json对象数据的方法
    PHP-----------HTTP请求的第三方接口
    PHP------数组和对象相互转化,stdClass Object转array
    include与require的区别
    PHP中exit()与die()的区别
  • 原文地址:https://www.cnblogs.com/duanjialin007/p/9058471.html
Copyright © 2011-2022 走看看