zoukankan      html  css  js  c++  java
  • python之使用request模块发送post和get请求

    import requests
    import json

    #发送get请求并得到结果
    # url = 'http://api.nnzhp.cn/api/user/stu_info?stu_name=小黑马 '#请求接口
    # req = requests.get(url)#发送请求
    # print(req.text)#获取请求,得到的是json格式
    # print(req.json())#获取请求,得到的是字典格式
    # print(type(req.text))
    # print(type(req.json()))

    #发送post请求,注册接口
    # url = 'http://api.nnzhp.cn/api/user/user_reg'
    # data = {'username':'mpp0130','pwd':'Mp123456','cpwd':'Mp123456'}
    # req = requests.post(url,data)#发送post请求,第一个参数是URL,第二个参数是请求数据
    # print(req.json())

    #入参是json
    # url = 'http://api.nnzhp.cn/api/user/add_stu'
    # data = {'name':'mapeipei','grade':'Mp123456','phone':15601301234}
    # req = requests.post(url,json=data)
    # print(req.json())

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

    # 添加cookie
    # url = 'http://api.nnzhp.cn/api/user/gold_add'
    # data = {'stu_id':231,'gold':123}
    # cookie = {'niuhanyang':'7e4c46e5790ca7d5165eb32d0a895ab1'}
    # req = requests.post(url,data,cookies=cookie)
    # print(req.json())

    #上传文件
    # url = 'http://api.nnzhp.cn/api/file/file_upload'
    # f = open(r'E:esttest epython-mppday7练习11.jpg','rb')
    # r = requests.post(url,files={'file':f})
    # users_dic = r.json()
    # print(users_dic)

    # 下载文件
    # url = 'http://www.besttest.cn/data/upload/201710/f_36b1c59ecf3b8ff5b0acaf2ea42bafe0.jpg'
    # r = requests.get(url)
    # print(r.status_code)#获取请求的状态码
    # print(r.content)#获取返回结果的二进制格式
    # fw = open('mpp.jpg','wb')
    # fw.write(r.content)
    # fw.close()

    #把浏览器页面下载到本地 保存网页,可以理解为简单的爬虫工具
    url='http://www.nnzhp.cn/archives/630'
    r = requests.get(url)
    f = open('nnzhp.html','wb')
    f.write(r.content)
    f.close()
  • 相关阅读:
    C++primer plus第六版课后编程题答案7.2
    sprintf函数
    sscanf函数
    [转]Visual Studio 2012 编译错误【error C4996: 'scanf': This function or variable may be unsafe. 】的解决方案
    C/C++动态二维数组的内存分配和释放
    malloc动态分配多维数组
    区间重合判断[poj2808 校门外的树]
    C++中的sort函数
    C语言qsort
    [转]Linux统计代码行数
  • 原文地址:https://www.cnblogs.com/mpp0905/p/8383522.html
Copyright © 2011-2022 走看看