前言
requests 發送請求返回的 html 頁面,默認是按 "ISO-8859-1" 編碼解碼,經常會出現返回的 html 出現亂碼的情況。
httprunner 2.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>', 博客園]
測試報告內容也會解碼