zoukankan      html  css  js  c++  java
  • python 网络编程

    r.text()  响应是文本
    r.content()无论响应是文本还是二进制内容,我们都可以用content属性获得bytes对象
    r.json() 对于特定类型的响应,例如JSON,可以直接获取

    一,发送get 请求

    import json ,requests
    url = 'http://api.nnzhp.cn/api/user/stu_info?stu_name=丁飞'
    req = requests.get(url) #发送get请求
    print(req.text) #获取结果
    print(type(req.text))#<class 'str'>
    print(req.json()) #获取结果直接就是字典,必须返回的是json串,才能用.json方法。
    print(type(req.json()))#<class 'dict'>

    二,发送post 请求

    import json ,requests
    url = 'http://api.nnzhp.cn/api/user/login'
    data = {'username':'niuhanyang','passwd':'aA123456'}
    req = requests.post(url,data) #发送post请求,第一个参数是url,第二个参数是请求的数据
    print(req.json())

    三,发送post请求,入参是json 格式

    import json ,requests
    url = 'http://api.nnzhp.cn/api/user/add_stu'
    data = {'name':'丁飞','grade':'巨蟹座','phone':31971891223}
    req = requests.post(url,json=data) #发送post请求,第一个参数是url,第二个参数是请求的数据
    print(req.json())

    四,添加 cookie

    import json ,requests
    url = 'http://api.nnzhp.cn/api/user/gold_add'
    data = {'stu_id':231,'gold':1000}
    cookie = {'niuhanyang':'6d195100b95a43046d2e385835c6e2c2'}
    req = requests.post(url,data,cookies=cookie)
    print(req.json())

    五,添加header

    import json ,requests
    url='http://api.nnzhp.cn/api/user/all_stu'
    header = {'Referer':'http://api.nnzhp.cn/','User-Agent':'Chore'}
    res = requests.get(url,headers=header)
    print(res.json())

    六,上传文件

    import json ,requests
    url = 'http://api.nnzhp.cn/api/file/file_upload'
    f = open(r'C:UsersjniuhanyangDesktopad.cpm.schedulingInfo.v1.json','rb')
    r = requests.post(url,files={'file':f})
    print(r.json())

    七,下载文件

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

    八,保存网页

    import json ,requests
    url = 'http://www.nnzhp.cn/archives/630'
    r = requests.get(url)
    f = open('nnzhp.html','wb')
    f.write(r.content)
    f.close()
  • 相关阅读:
    js获取前一页面连接的参数值
    window.onload()函数和jQuery中的document.ready()有什么区别
    jquery中$.get()提交和$.post()提交有区别吗?
    JQuery有几种选择器?
    jQuery 库中的 $() 是什么?
    JavaScript内置可用类型
    .JS 中 == 和 === 区别是什么
    undefined,null 和 undeclared 有什么区别
    JS中如何将页面重定向到另一个页面
    数据库设计中,一对多如何处理?
  • 原文地址:https://www.cnblogs.com/chendai21/p/8383787.html
Copyright © 2011-2022 走看看