httprunner 2.x学习15 - response 返回 html 页面解码

前言

requests 发送请求返回的 html 页面,默认是按 “ISO-8859-1” 编码解码,经常会出现返回的 html 出现乱码的情况。
httprunner 3.x可以在debugtalk.py 写个hook函数解码返回的html内容
response 解码

requests 直接请求页面,返回的html里面有乱码

import requests url = "https://home.cnblogs.com/u/yoyoketang/" r = requests.get(url) print(r.encoding) print(r.text)

运行结果

ISO-8859-1 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"/> <meta http-equiv="X-UA-Compatible" content="IE=EDGE"/> <title>博客园</title> <base href="/"/> <meta name="viewport" content="width=device-width, initial-scale=1"/> <link rel="icon" type="image/x-icon" href="//common.cnblogs.com/favicon.ico"/> <meta name="description" content="博客园是一个面向开发者的知识分享社区。"/> <meta name="og:description" content="博客园是一个面向开发者的知识分享社区。"/> <link rel="stylesheet" href="styles.18055be4eba6aa87ad55.css"></head>

解决办法可以指定 response 的解码格式

import requests # 作者-上海悠悠 QQ交流群:717225969 # blog地址 https://www.cnblogs.com/yoyoketang/ url = "https://home.cnblogs.com/u/yoyoketang/" r = requests.get(url) r.encoding = "utf-8" # 解码方式 print(r.encoding) print(r.text)

于是就能看到正常的html内容了

utf-8 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"/> <meta http-equiv="X-UA-Compatible" content="IE=EDGE"/> <title>博客园</title> <base href="/"/> <meta name="viewport" content="width=device-width, initial-scale=1"/> <link rel="icon" type="image/x-icon" href="//common.cnblogs.com/favicon.ico"/> <meta name="description" content="博客园是一个面向开发者的知识分享社区。"/> <meta name="og:description" content="博客园是一个面向开发者的知识分享社区。"/>

hrun 2.x问题描述

在httprunner2.x 中看到正则表达式没法提取乱码

FAIL: test_0000_000 (httprunner.api.TestSequense) test demo case1 ---------------------------------------------------------------------- Traceback (most recent call last): File "e:\python36\lib\site-packages\httprunner\api.py", line 63, in test test_runner.run_test(test_dict) httprunner.exceptions.ValidationFailure: validate: <title>(.+?)</title> equals 博客园(str) ==> fail 博客园(str) equals 博客园(str) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "e:\python36\lib\site-packages\httprunner\api.py", line 65, in test self.fail(str(ex)) AssertionError: validate: <title>(.+?)</title> equals 博客园(str) ==> fail 博客园(str) equals 博客园(str) ----------------------------------------------------------------------

teardown_hook解码

在debugtalk.py 中写一个hook函数解码返回的response内容

# 作者-上海悠悠 QQ交流群:717225969 # blog地址 https://www.cnblogs.com/yoyoketang/ def response_decode(response): """解码返回的html内容""" print(response) print(response.resp_obj) # requests.response response.resp_obj.encoding = "utf-8"

response.resp_obj 就是 requests 库的 response 对象

yaml脚本如下

# 作者-上海悠悠 QQ交流群:717225969 # blog地址 https://www.cnblogs.com/yoyoketang/ config: name: yoyoketang teststeps: - name: yoyoketang request: url: https://home.cnblogs.com/u/yoyoketang/ method: GET headers: User-Agent: Fiddler Content-Type: application/json verify: false teardown_hooks: - ${response_decode($response)} validate: - eq: [status_code, 200] - eq: ['<title>(.+?)</title>', 博客园]

测试报告内容也会解码

(0)

相关推荐