• Python + Requests 知识点回顾


    http://www.downza.cn/soft/11145.html PS下载地址
    http://www.bejson.com/jsonviewernew/ JSON的在线视图

    import requests

    url = 'https://movie.douban.com'
    url1 = 'https://search.douban.com/movie/subject_search'
    r = requests.get(url='https://movie.douban.com')

    paydata = {'search_text':'Teacher','cat':1002}
    r = requests.get(url=url1,params=paydata)

    print(r.status_code) #状态码
    print(r.text) #响应内容
    print(r.content.decode('utf')) #字节流 把字节转换为中文
    print(r.url) #url
    print(r.encoding) #编码格式



    import requests
    import json

    url = 'https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false'

    headers = {
        'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
        'Cookie':'JSESSIONID=ABAAABAABFIAAACEBE8AD07641E9CDE63BF0812AA182306; __guid=237742470.3752759870354819600.1580823932255.3586; WEBTJ-ID=20200204214542-17010731db87d-018d0092af1e6a-454c092b-1049088-17010731db91a; _ga=GA1.2.1788274808.1580823946; _gid=GA1.2.1421117658.1580823946; user_trace_token=20200204214547-a60e97c9-4754-11ea-af23-5254005c3644; LGUID=20200204214547-a60e9b5c-4754-11ea-af23-5254005c3644; index_location_city=%E5%85%A8%E5%9B%BD; TG-TRACK-CODE=index_search; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%221701073f332163-0d4ab9c8e5f6be-454c092b-1049088-1701073f3331f%22%2C%22%24device_id%22%3A%221701073f332163-0d4ab9c8e5f6be-454c092b-1049088-1701073f3331f%22%7D; X_MIDDLE_TOKEN=60e32c1f76d7949967ac9ef5e5aaabef; lagou_utm_source=A; Hm_lvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1580823946,1580824068,1580913010; Hm_lpvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1580915924; SEARCH_ID=68e06e018ddc43b2a8d829f0e10c8855; monitor_count=33; X_HTTP_TOKEN=c8a95705e204e59a40175908510f64c31ff8f001a4; _gat=1; PRE_UTM=; PRE_HOST=; PRE_LAND=https%3A%2F%2Fwww.lagou.com%2Fjobs%2Flist%5F%25E8%2587%25AA%25E5%258A%25A8%25E5%258C%2596%25E6%25B5%258B%25E8%25AF%2595%2Fp-city%5F0%3F%26cl%3Dfalse%26fromSearch%3Dtrue%26labelWords%3D%26suginput%3D; LGSID=20200206104504-ce133552-e0ef-48b8-af92-9aa2182c2ed7; PRE_SITE=https%3A%2F%2Fwww.lagou.com%2Futrack%2FtrackMid.html%3Ff%3Dhttps%253A%252F%252Fwww.lagou.com%252Fjobs%252Flist%255F%2525E8%252587%2525AA%2525E5%25258A%2525A8%2525E5%25258C%252596%2525E6%2525B5%25258B%2525E8%2525AF%252595%252Fp-city%255F0%253F%2526cl%253Dfalse%2526fromSearch%253Dtrue%2526labelWords%253D%2526suginput%253D%26t%3D1580957097%26%5Fti%3D1; LGRID=20200206104504-58c5e35a-ca62-4bfa-a6b4-564bb611cc0e',
        'Referer':'https://www.lagou.com/jobs/list_%E8%87%AA%E5%8A%A8%E5%8C%96%E6%B5%8B%E8%AF%95/p-city_0?&cl=false&fromSearch=true&labelWords=&suginput=',
        'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
    }

    data = {
        'first':'false',
        'pn':2,
        'kd':'自动化测试',
        'sid':'8fc0bd4a8ae7471f8b48179e78ef804f'
    }

    r = requests.post(url=url,data=data,headers=headers,timeout=0.3)
    print(r.status_code)
    print(type(r.json()))
    print(json.dumps(r.json(),indent=True,ensure_ascii=False))

    post 中data和json的使用,主要看请求头中的 Content-Type类型 data = json.dumps(data)或者 json=data

    print(r.json()['content']['positionResult']['result'][0]['companyFullName'])



    cookie的处理案例

    import requests
    url = 'http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=202014161691'

    def getheaders():
        headers = {
            'Content-Type':'application/x-www-form-urlencoded',
            'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
            # 'Cookie':'anonymid=k6agx27k-4y637m; depovince=GW; _r01_=1; JSESSIONID=abcCqJP9pvVyoaG_1Kzax; ick_login=5decb7b1-44f7-4e7f-9ab9-584be1318cb6; __guid=238633222.2841094300608286700.1580977055396.5442; taihe_bi_sdk_uid=fb8853833de6eb33167c9d33f92c8545; taihe_bi_sdk_session=ee8126229981f64001f01b1b123280c4; first_login_flag=1; ln_uact=18797815816; ln_hurl=http://hdn.xnimg.cn/photos/hdn221/20181215/2150/h_main_BQJL_13bc000f55f11986.jpg; loginfrom=null; springskin=set; jebe_key=750789ec-ee20-4f87-88af-ae4136fa7650%7C59d6a45b40f5eff03ceca5fd44f241d8%7C1580977144984%7C1%7C1580977144921; jebe_key=750789ec-ee20-4f87-88af-ae4136fa7650%7C59d6a45b40f5eff03ceca5fd44f241d8%7C1580977144984%7C1%7C1580977144941; vip=1; wp_fold=0; jebecookies=71d7a58a-12f3-410d-ab44-7f2e34ed3d86|||||; monitor_count=3'
        }
        return headers

    def Data():
        data = {
            'email':'18797815816',
            'autoLogin':'true',
            'icode':'ST7T',
            'origURL':'http://www.renren.com/home',
            'domain':'renren.com',
            'key_id':1,
            'captcha_type':'web_login',
            'password':'71e92cbb2659c9c92a9797b193b889f1791cc51f3387f0fae3e6c71329e322a7',
            'rkey':'14bf33ff3fade143d5f83f99e09174f8',
            'f':''
        }
        return data

    def login():
        r = requests.post(url=url,data=Data(),headers=getheaders())
        # print(r.content.decode('utf-8'))
        return r.cookies  

    print(login())

    def getProfile():
        #查看个人主页
        url = 'http://www.renren.com/969120248/profile'
        r = requests.get(url=url,cookies=login())
        print(r.text)

    getProfile()


    token的处理
    处理的思路,产品登录成功后,取出token,给下一个请求参数传入token


    Auth 鉴权的处理
    import requests
    from requests.auth import HTTPBasicAuth

    r = requests.get(url='http://127.0.0.1:5000/v1/api/books',auth=HTTPBasicAuth('Admin','admin'))

    print(r.json()['data'][0])


    session 的处理思路

    import requests
    url = 'http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=202014161691'

    def getheaders():
        headers = {
            'Content-Type':'application/x-www-form-urlencoded',
            'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
            # 'Cookie':'anonymid=k6agx27k-4y637m; depovince=GW; _r01_=1; JSESSIONID=abcCqJP9pvVyoaG_1Kzax; ick_login=5decb7b1-44f7-4e7f-9ab9-584be1318cb6; __guid=238633222.2841094300608286700.1580977055396.5442; taihe_bi_sdk_uid=fb8853833de6eb33167c9d33f92c8545; taihe_bi_sdk_session=ee8126229981f64001f01b1b123280c4; first_login_flag=1; ln_uact=18797815816; ln_hurl=http://hdn.xnimg.cn/photos/hdn221/20181215/2150/h_main_BQJL_13bc000f55f11986.jpg; loginfrom=null; springskin=set; jebe_key=750789ec-ee20-4f87-88af-ae4136fa7650%7C59d6a45b40f5eff03ceca5fd44f241d8%7C1580977144984%7C1%7C1580977144921; jebe_key=750789ec-ee20-4f87-88af-ae4136fa7650%7C59d6a45b40f5eff03ceca5fd44f241d8%7C1580977144984%7C1%7C1580977144941; vip=1; wp_fold=0; jebecookies=71d7a58a-12f3-410d-ab44-7f2e34ed3d86|||||; monitor_count=3'
        }
        return headers

    def Data():
        data = {
            'email':'18797815816',
            'autoLogin':'true',
            'icode':'ST7T',
            'origURL':'http://www.renren.com/home',
            'domain':'renren.com',
            'key_id':1,
            'captcha_type':'web_login',
            'password':'71e92cbb2659c9c92a9797b193b889f1791cc51f3387f0fae3e6c71329e322a7',
            'rkey':'14bf33ff3fade143d5f83f99e09174f8',
            'f':''
        }
        return data

    def login():
        s = requests.Session()
        r = s.post(url=url,data=Data(),headers=getheaders())
        # print(r.content.decode('utf-8'))
        return s  

    login()

    def getProfile():
        #查看个人主页
        url = 'http://www.renren.com/969120248/profile'
        r = login().get(url=url,cookies=login())
        print(r.text)

    getProfile()


    图片上传操作

    import requests

    url = 'http://upload.renren.com/upload.fcgi?pagetype=addpublishersingle&hostid=969120248&callback=window.parent.handlePhotoData&uploadid=profile_publisher_photo_1581055343406'

    def getheaders():
        headers = {
            'Content-Type':'multipart/form-data;',
            'Cookie':'anonymid=k6agx27k-4y637m; depovince=GW; _r01_=1; taihe_bi_sdk_uid=fb8853833de6eb33167c9d33f92c8545; ln_uact=18797815816; ln_hurl=http://hdn.xnimg.cn/photos/hdn221/20181215/2150/h_main_BQJL_13bc000f55f11986.jpg; springskin=set; jebe_key=750789ec-ee20-4f87-88af-ae4136fa7650%7C59d6a45b40f5eff03ceca5fd44f241d8%7C1580977144984%7C1%7C1580977144921; _de=6B7940F5B97ED7B17DBB8F9AC51F78BA; p=f3b40f4c426a9ae54444d012b249304c8; ap=969120248; first_login_flag=1; t=88bddb94a692968e32e040a92f26bfb68; societyguester=88bddb94a692968e32e040a92f26bfb68; id=969120248; xnsid=e233e0a0; loginfrom=syshome; wp_fold=0; taihe_bi_sdk_session=b66e2aa58a8e974c83a9ab6696905589; XNESSESSIONID=606c5f9e7654',
            'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
        }
        return headers

    def Data():
        data = {
            'title':'',
            'upload':'提交',
            '__channel':'renren',
            'privacyParams':'{"sourceControl": 99}',
            'hostid':'969120248',
            'requestToken':'-71562601',
            '_rtk':'c77d08f1',
        }
        return data

    files = {'file':('xx.png',open(r'C:UserslenovoDesktopsucaixx.png','rb'),'image/png',{})}

    r = requests.post(url=url,data=Data(),headers=getheaders(),files=files)
    print(r.status_code)
    print(r.content.decode('utf-8'))


    文件的下载

    import requests
    import shutil

    def headers():
        pass

    def Data():
        pass

    r = requests.post(url=url,headers,data=data,stream=True)
    if r.status_code == 200:
        pass


    postman

    安装Newman 先安装node.js,把npm添加到环境变量中。
    npm install newman --registry=https://registry.npm.taobao.org
    newman run 拉钩网.postman_collection.json newman-reporter-html


    login登录成功后是不是代表业务流程是对的?
    不是,只是代表login的接口是对的。

    接口测试的维度:
    1.请求参数的边界值
    2.请求参数为空
    3.请求参数数据类型
    4.请求&响应时间的验证
    5.基本的业务流程,业务流程ok,前后端业务场景测试ok

    不需要单独的验证数据库的数据是否正确
    因为客户端向服务端发起请求获取数据,服务端链接到数据库,写SQL语句,然后返回需要的内容给客户端

    500万请求数据,怎么断言数据,服务器会链接SQL语句,把消费的金额进行累计进行断言。
  • 相关阅读:
    怎样用Lodrunner测试WAP站点的性能(两种解决方案)
    LR测试登陆后进行的操作时 绕过登录
    单元测试 模块接口测试
    一个分布式服务器集群架构方案
    利用loadrunner代理方式,录制手机APP脚本
    安卓开发环境安卓
    安卓自动化测试(2)Robotium环境搭建与新手入门教程
    安卓自动化测试(1)安卓自动化测试原理概念
    HTML框架
    HTML5表单提交与PHP环境搭建
  • 原文地址:https://www.cnblogs.com/Teachertao/p/12292836.html
Copyright © 2020-2023  润新知