简体   繁体   English

如何从Django中的web页面获取html

[英]How to get html from web page in Django

I was trying to implement some testing in Django, and I needed to check the data on the web page with a string, but I couldn't retrieve data from the web page.我想在Django实现一些测试,我需要用字符串查看web页面的数据,但我无法从web页面检索数据。 so, how can I get data(html) from web page?那么,如何从 web 页面获取数据(html)?

my tests.py我的测试.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")

What you're doing is correct.你在做什么是正确的。 You can retrieve data from web page by using response.content .您可以使用response.content从 web 页面检索数据。 The problem is that you're missing a slash at the end of the url, so the url should be问题是你在 url 的末尾少了一个斜杠,所以 url 应该是

 response = self.client.get(f"/api/ticket/1/")

instead of代替

 response = self.client.get(f"/api/ticket/1")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM