前言
小编最近在编写接口自动化测试用例的过程中,需要将get请求url中的部分参数替换成预设的数据,将url中的具有时效性的auth替换成auth生成方法返回值。经过一番调研,最后选取了python的urllib库的parse模块。
urllib.parse 模块提供了一系列用于操纵 URLs 地址及其各组成部分的函数,这些函数或者用于拆分或者用于组装。
urllib.parse函数介绍
分析:
1.ulrparse()
函数的返回值是一个 ParseResult 对象,该对象与含有六个元素的元组 tuple 类似。
urllib_parse_urlparse.py from urllib.parse import urlparse url = 'http://test.dis.e.sogou/adlist?offset=0&auth=69CF80EA062863279B72612FA5443B6F&requestId=0025500016111592878436805&count=1&network=1' parsed = urlparse(url) print(parsed) |
可以通过元组索引的方式获取的 URL 地址的六个部分为:方案 (scheme) ,网址 (network location) ,路径 (path) ,路径段 (path segment) 参数(用分号与路径隔开),查询字符串 (query) 和片段 (fragment) 。
python3 urllib_parse_urlparse.py ParseResult(scheme='http', netloc='test.dis.e.sogou', path='/adlist', params='', query='offset=0&auth=69CF80EA062863279B72612FA5443B6F&requestId=0025500016111592878436805&count=5&model=2&terminal=3&network=1', fragment='') |
2.urlsplit()
urlsplit() 函数可以作为 urlparse() 的一个替代选择,但不会拆分 URL 里的参数。
逆解析:
1.geturl()
要把拆分后的 URL 的各部分重新组装回去,得到一个完整 URL 字符串的方法不止有一种。解析后的 URL 对象有一个 geturl() 方法。
urllib_parse_geturl.py from urllib.parse import urlparse original = 'http://test.dis.e.sogou/adlist?offset=0&auth=69CF80EA062863279B72612FA5443B6F&requestId=0025500016111592878436805&count=5&model=2&terminal=3&network=1' print('ORIG :', original) parsed = parse.urlparse(original) print('PARSED:', parsed.geturl()) |
$ python3 urllib_parse_geturl.py ORIG : http://test.dis.e.sogou/adlist?offset=0&auth=69CF80EA062863279B72612FA5443B6F&requestId=0025500016111592878436805&count=5&model=2&terminal=3&network=1 PARSED: http://test.dis.e.sogou/adlist?offset=0&auth=69CF80EA062863279B72612FA5443B6F&requestId=0025500016111592878436805&count=5&model=2&terminal=3&network=1 |
geturl() 只对 urlparse() 或 urlsplit() 返回的对象有效。
2.ulrunparse()
可以用 urlunparse() 将一个常规的字符串元组组装为一个 URL 地址。
拼接:
1.urljoin()
urllib.parse 模块中除了解析 URLs 用的 urlparse() 函数,它还包含 urljoin() 函数,可以用它从相对地址的片段中创建出绝对 URLs 地址 。
urllib_parse_urljoin.py from urllib.parse import urljoin print(urljoin('http://www.example.com/path/file.html', 'anotherfile.html')) print(urljoin('http://www.example.com/path/file.html', '../anotherfile.html')) |
在本例中,在拼接第二个 URL 的时候,表示相对路径的 ("../") 被考虑在内。
$ python3 urllib_parse_urljoin.py http://www.example.com/path/anotherfile.html http://www.example.com/anotherfile.html |
非相对路径则以 os.path.join() 的方式同样处理。
urllib_parse_urljoin_with_path.py print(urljoin('http://www.example.com/path/', '/subpath/file.html')) print(urljoin('http://www.example.com/path/', 'subpath/file.html')) |
如果要被拼接到 URL 地址的路径以斜杠 (/) 开始,那么就以该路径作为顶层重设 URL 地址。否则,它仅仅被添加到 URL 路径尾部。.
$ python3 urllib_parse_urljoin_with_path.py http://www.example.com/subpath/file.html http://www.example.com/path/subpath/file.html |
编码查询参数:
1.ulrencode()
查询参数必须在编码后才能加入 URL 地址
urllib_parse_urlencode.py from urllib.parse import urlencode query_args = { 'q': 'query string', 'foo': 'bar', } encoded_args = urlencode(query_args) print('Encoded:', encoded_args) |
编码过程将替换一些特殊字符,比如空格,以保证传递给服务器的查询字符串的格式是符合标准的。
$ python3 urllib_parse_urlencode.py Encoded: q=query+string&foo=bar |
在查询字符串中,为了让一序列变量值中的每一个以单独的方式出现,可以在调用 urlencode() 的时候将 doseq 设为 True 。
2.parse_qs()
parse_qs() 返回的结果是一个字典,字典的每一项都是一个查询名称与其对应的(一个或多个)值的列表, 而 parse_qsl() 返回一个元组的列表,每个元组是一对查询名称与查询值
$ python3 urllib_parse_parse_qs.py parse_qs : {'foo': ['foo1', 'foo2']} parse_qsl: [('foo', 'foo1'), ('foo', 'foo2')] |
ulrlib.parse在框架中的使用
test_dippatcher_adlist.py url='http://test.dis.e.sogou/adlist?offset=0&auth=69CF80EA062863279B72612FA5443B6F&requestId=0025500016111592878436805&count=3&model=ios&terminal=1&version=2&network=1'http://test.dis.e.sogou/adlist?offset=0&auth=69CF80EA062863279B72612FA5443B6F&requestId=0025500016111592878436805&count=3&model=ios&terminal=1&version=2&network=1' # 获取request_id和auth request_id = generate_requestId(expect['platformId'], expect['posId']) auth = generate_auth(request_id, expect['token']) # 修改Url中参数,替换request_id和auth # 分析URL url_parsed = parse.urlparse(url) bits = list(url_parsed) qs = parse.parse_qs(bits[4]) # 替换qs中接口入参 qs['requestId'] = request_id qs['auth'] = auth qs['offset'] = expect['offset'] qs['count'] = expect['count'] qs['model'] = expect['model'] qs['terminal'] = expect['terminal'] qs['version'] = expect['version'] qs['network'] = expect['network'] # 编辑查询参数 bits[4] = parse.urlencode(qs) # URL逆解析 url_new = parse.urlunparse(bits) print(url_new) |
为了更好的理解,将每一部分的结果都输出。
$ python3 test_dispatcher_adlist.py bits: ['http', 'test.dis.e.sogou', '/adlist', '', "offset=0&auth=69CF80EA062863279B72612FA5443B6F&requestId=0025500016111592878436805&count=3&model=ios&terminal=1&version=2&network=1'http://test.dis.e.sogou/adlist?offset=0&auth=69CF80EA062863279B72612FA5443B6F&requestId=0025500016111592878436805&count=3&model=ios&terminal=1&version=2&network=1", ''] qs: {'offset': ['0'], 'auth': ['69CF80EA062863279B72612FA5443B6F', '69CF80EA062863279B72612FA5443B6F'], 'requestId': ['0025500016111592878436805', '0025500016111592878436805'], 'count': ['3', '3'], 'model': ['ios', 'ios'], 'terminal': ['1', '1'], 'version': ['2', '2'], 'network': ["1'http://test.dis.e.sogou/adlist?offset=0", '1']} bits[4]: offset=0&auth=8215f55af287a62a29efe7a70fd3ba0d&requestId=0025500016111593405114583&count=1&model=eee&terminal=1&version=eee&network=1 http://test.dis.e.sogou/adlist?offset=0&auth=8215f55af287a62a29efe7a70fd3ba0d&requestId=0025500016111593405114583&count=1&model=eee&terminal=1&version=eee&network=1 |
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。