python - 如何从Django中的网页获取html
问题描述
我试图在 Django 中实现一些测试,我需要用字符串检查网页上的数据,但我无法从网页中检索数据。那么,如何从网页获取数据(html)?
我的测试.py
from rest_framework.test import APITestCase
class TicketTestCase(APITestCase):
def test_user_details_on_ticket_id(self):
response = self.client.get(f"/api/ticket/1")
print(response.content) # this returns empty data in bytes
# I want to check this
#self.assertEquals(response.data, "helloworld")
解决方案
你在做什么是正确的。您可以使用response.content
. 问题是您在网址末尾缺少斜杠,因此网址应该是
response = self.client.get(f"/api/ticket/1/")
代替
response = self.client.get(f"/api/ticket/1")
推荐阅读
- python - Dask.分布式集群管理
- javascript - ..._1.default 在使用 Mocha 测试 TypeScript 时不是构造函数
- c++ - 数组索引超出范围,但 gdb 报告错误的行 - 为什么?
- django - 在 Django annotate 中执行三角运算或任何数学表达式
- maven - 加快构建 EAR 文件并跳过构建没有更改的 WAR 文件
- c++ - c++中linux命令的索引输出
- html - 应用最小高度时,容器 div 内的 div 消失
- electron - 如何在 electron.js 中修复“跳过 checkForUpdatesAndNotify,因为应用程序未打包”
- windows - 在 Windows 窗体上不按 ALT 键的情况下执行按钮快捷方式
- bash - 带有参数的远程源的 bash 配置文件