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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值