zoukankan      html  css  js  c++  java
  • python自动化测试学习笔记-6urllib模块&request模块

    python3的urllib 模块提供了获取页面的功能。

    urllib.request.urlopen(urldata=None[timeout]*cafile=Nonecapath=Nonecadefault=Falsecontext=None)

    -         url:  需要打开的网址

    -         data:Post提交的数据

    -         timeout:设置网站的访问超时时间

    直接用urllib.request模块的urlopen()获取页面,page的数据格式为bytes类型,需要decode()解码,转换成str类型。

    import urllib.request

    # import json
    # import requests
    url="http://api.nnzhp.cn/api/user/stu_info?stu_name=xiaohei"
    req=urllib.request.urlopen(url)
    res=req.read().decode()
    print(res)

    执行:

    {
            "error_code": 2,
            "msg": "无结果"
    }

    urllib 中实现post数据请求

    urlopen()的data参数默认为None,当data参数不为空的时候,urlopen()提交方式为Post。

    url1='http://api.nnzhp.cn/api/user/login '
    data={
    'username':'niuhanyang',
    'passwd':'aA123456'
    }
    #urlencode()主要作用就是将url附上要提交的数据。经过urlencode()转换后的data数据为?username=niuhanyang&passwd=aA123456,
    ## Post的数据必须是bytes或者iterable of bytes,不能是str,因此需要进行encode()编码
    data=urllib.parse.urlencode(data).encode('utf-8')
    #最终提交的url是http://api.nnzhp.cn/api/user/login?username=niuhanyang?passwd=aA123456
    req=urllib.request.Request(url1,data=data)
    page=urllib.request.urlopen(req).read()
    print(page.decode())

    执行查看结果:

    {
            "error_code": 0,
            "login_info": {
                    "login_time": "20180129202722",
                    "sign": "7e4c46e5790ca7d5165eb32d0a895ab1",
                    "userId": 1
            }
    }

    我们看到使用urllib会比较麻烦,需要转码,赋值等操作,request模块可以更加简便的完成请求操作,如下:

    1、首先需要安装Request模块

    pip install requests

    2、导入request模块

    import requests

    各种接口操作如下:

    import requests
    import json
    #发送无参数的get请求
    url='http://www.baidu.com'
    req=requests.get(url)
    print(req.text)#返回的字符串类型

    #发送有参数的request请求
    url1='http://api.nnzhp.cn/api/user/stu_info?stu_name=feifei'
    req1=requests.get(url1)
    print(req1.json())#返回的字典列表

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

    #发送入参是json类型的post请求
    url3='http://api.nnzhp.cn/api/user/add_stu'
    data={
    'name':'feifei',
    'phone':'13121111112',
    'grade':'1000'
    }

    req=requests.post(url3,json=data)
    print(req.json())

    #发送带有cookie的post请求
    #添加cookie
    url4='http://api.nnzhp.cn/api/user/gold_add'
    data={
    'stu_id':230,
    'gold':88888
    }
    cookies={'feifei':'a2b454c3830e20e7d9916f6b52d6a3a7'}
    req=requests.post(url4,data,cookies=cookies)
    print(req.json())

    #发送带有Referer请求的post请求
    #
    url5='http://api.nnzhp.cn/api/user/all_stu'
    data={
    'Referer':'http://api.nnzhp.cn/'
    }
    req=requests.get(url5,headers=data)
    print(req.json())

    #下载文件请求

    url6='https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1517138333609&di=327abc49fc6d63fed19124cdf826d130&imgtype=0&src=http%3A%2F%2Fimg4.duitang.com%2Fuploads%2Fitem%2F201510%2F17%2F20151017223821_ZSWBc.jpeg'
    r=requests.get(url6)#下载直接请求url然后进行保存
    #print(r.status_code)#请求状态码是二进制
    res=r.content#获取二进制格式
    fw=open('feifei.jpg','wb')
    fw.write(res)#保存文件
    fw.close()

    #上传文件

    url7='http://api.nnzhp.cn/api/file/file_upload'
    f=open('E:\besttest\python\besttest_code\练习\day7笔记\api\feifei.jpg','rb')
    r=requests.post(url7,files={'file':f})
    print(r.json())
    #
    #下载页面
    url='http://www.runoob.com/python/python-intro.html'
    r=requests.get(url)
    f=open('python.html','wb')
    f.write(r.content)
    f.close()


























  • 相关阅读:
    JavaScript数据类型(第一天)
    数据库的事务隔离级别
    SSH(远程登录)原理
    Hadoop安装
    activiti学习第二天
    activiti学习第一天
    .net调用系统软键盘(兼容win7及win10)
    使用Blend设计出符合效果的WPF界面
    Mysql的收获
    PHP文件上传安全处理的步骤
  • 原文地址:https://www.cnblogs.com/phoebes/p/8379490.html
Copyright © 2011-2022 走看看