【问题标题】:In Python 3.7/Django 3, how can I get the rendered HTML of a page in a web browser?在 Python 3.7/Django 3 中,如何在 Web 浏览器中获取页面呈现的 HTML?
【发布时间】:2020-11-01 07:13:45
【问题描述】:

我正在使用 Python 3.7 和 Django 3。我目前使用以下代码生成具有给定 URL 的 Web 浏览器

filePath = '/tmp/cat_and_dog.webp'
searchUrl = 'http://www.google.hr/searchbyimage/upload'
multipart = {'encoded_image': (filePath, open(filePath, 'rb')), 'image_content': '', }
response = requests.post(searchUrl, files=multipart, allow_redirects=False)
fetchUrl = response.headers['Location']
webbrowser.open(fetchUrl)

如何获取渲染的 DOM 的 HTMl?请注意,这与“查看源代码”显示的内容不同。我对在浏览器中运行“检查元素”时看到的 DOM 版本更感兴趣。我可能使用了不正确的术语,但希望它清楚我希望提取浏览器呈现的实际 HTML。

【问题讨论】:

    标签: python html django render python-webbrowser


    【解决方案1】:

    也许你可以使用 render_to_string 函数

    from django.template.loader import render_to_string
    
    def some_function():
    
        rendered_template = render_to_string('template_name.html','context')
    

    【讨论】:

    • 我没有连接这些点。我如何从我的“webbrowser.open(fetchUrl)”调用(或等效调用)到你所拥有的?
    猜你喜欢
    • 2011-01-06
    • 1970-01-01
    • 2013-09-14
    • 2021-07-25
    • 2018-09-04
    • 2013-12-03
    • 1970-01-01
    • 1970-01-01
    • 2014-02-13
    相关资源
    最近更新 更多