• requests模块


    import requests
    
    #1、发get请求
    url = 'http://apixxxxx.api/user/stu_info'
    data = {'stu_name':'小黑'}  #请求数据
    req = requests.get(url,params=data)  #发get请求
    print(req.json())  #字典
    print(req.text)    #string,json串
    print(req.content) #2进制内容
    #2 、发post请求
    url = 'http://api.xxxx.cn/api/user/login'
    data = {'username':'lixi','passwd':'aA123456'}  #请求数据
    req = requests.post(url,data)  #发送post请求
    print(req.json())
    print(req.text)
    #3、入参是json类型的
    import random
    phone=random.randint(10000000000,99999999999)
    url='http://api.xxxx.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.xxxx.cn/api/user/gold_add'
    data = {'stu_id':468,'gold':10000}
    djl = {'xxxx':'337ca4cc825301654676151f9dc4bc6'}
    req = requests.post(url,data,cookies=djl)
    print(req.json())
    #5、添加header
    url = 'http://api.xdasda.cn/api/user/all_stu'
    header = {
        'Referer':'http://api.xadasds.cn/'
    }
    req = requests.get(url,headers=header)
    print(req.json())
    #6、上传文件
    url= 'http://api.xxxas.cn/api/file/file_upload'
    data = {
        'file':open(r'C:UsersxadsasDesktop图6bd9026dt935575932465&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)
  • 相关阅读:
    二叉树操作
    jQuery下拉列表插件 jQselectable
    DeDeCMS常用标签代码整理汇总
    用DEDECMS做手机网站
    DedeCMS模板文件结构
    用aspnet_compiler编译(发布)网站
    把网站提交到搜索引擎
    jGrowl 制作消息弹出框
    关于标签 XUACompatible
    jQuery下拉框插件 FlexBox
  • 原文地址:https://www.cnblogs.com/xinjing-jingxin/p/9674403.html
Copyright © 2020-2023  润新知