httprunner 2.x學習15 - response 返回 html 頁面解碼
阿新 • • 發佈:2021-06-24
前言
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> regex_match 上海-悠悠 - 部落格園(str) ==> fail å客å(str) regex_match 上海-悠悠 - 部落格園(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> regex_match 上海-悠悠 - 部落格園(str) ==> fail å客å(str) regex_match 上海-悠悠 - 部落格園(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>', 部落格園]