• python网络编程(requests)


    import json,requests


    requests比较好用,不要用urllib

    #发送get请求
    # url = 'http://api.nnzhp.cn/api/user/stu_info?stu_name=小黑马'
    # req = requests.get(url) #发送get请求
    # print(req.text) #获取结果,返回字符串
    # print(req.json()) #获取结果直接就是字典,必须返回的是json串,才能用.json方法。

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

    #入参是json的
    # 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

    # 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
    # url='http://api.nnzhp.cn/api/user/all_stu'
    # mpp = {'Referer':'http://api.nnzhp.cn/','User-Agent':'Chore'}
    # res = requests.get(url,headers=mpp)
    # print(res.json())

    #上传文件
    # 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())

    #下载文件(图片,音频、视频)
    # 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()

    #保存网页
    url = 'http://www.nnzhp.cn/archives/630'
    r = requests.get(url)
    f = open('nnzhp.html','wb')
    f.write(r.content)
    f.close()

  • 相关阅读:
    [TJOI2007]小朋友
    弦图小结
    Bzoj2141: 排队
    [SYZOI Round1] 滑稽♂树
    Bzoj2244: [SDOI2011]拦截导弹
    Bzoj1492: [NOI2007]货币兑换Cash(不单调的斜率优化)
    Bzoj1495: [NOI2006]网络收费
    Bzoj1496: [NOI2006]千年虫
    Bzoj1498&1416: [NOI2006]神奇的口袋
    Bzoj4553: [Tjoi2016&Heoi2016]序列
  • 原文地址:https://www.cnblogs.com/yzhuahai/p/12642380.html
Copyright © 2020-2023  润新知