繁体   English   中英

从 python 代码在浏览器中显示 html 代码

[英]Displaying html code in browser from python code

我尝试运行此代码:

try:
    resp = urllib.request.urlopen('https://drive.google.com/uc?id=1bRM_HKJOxTowYfoCz87e2R7dk3Zp4Zyp')
    contents = resp.read()
except urllib.error.HTTPError as error:
    contents = error.read()
    print(contents)

因为该链接来自谷歌驱动器,所以它对可以访问的数量有限制,并且我收到的错误是 HTML 代码的形式,如下所示:

b'<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"/><title>Sorry...</title><style> body { font-family: verdana, arial, sans-serif; background-color: #fff; color: #000; }</style></head><body><div><table><tr><td><b><font face=sans-serif size=10><font color=#4285f4>G</font><font color=#ea4335>o</font><font color=#fbbc05>o</font><font color=#4285f4>g</font><font color=#34a853>l</font><font color=#ea4335>e</font></font></b></td><td style="text-align: left; vertical-align: bottom; padding-bottom: 15px; width: 50%"><div style="border-bottom: 1px solid #dfdfdf;">Sorry...</div></td></tr></table></div><div style="margin-left: 4em;"><h1>We\'re sorry...</h1><p>... but your computer or network may be sending automated queries. To protect our users, we can\'t process your request right now.</p></div><div style="margin-left: 4em;">See <a href="https://support.google.com/websearch/answer/86640">Google Help</a> for more information.<br/><br/></div><div style="text-align: center; border-top: 1px solid #dfdfdf;"><a href="https://www.google.com">Google Home</a></div></body></html>' 

如何从 python 代码内部在浏览器中运行此 HTML 代码? 并且错误消息总是在 HTML 代码中吗? 或者这只是谷歌驱动器案例的情况? 抱歉英语不好

当驱动器分析它的机器人命令被注入驱动器时,您会收到此错误。此错误与所有不支持自动查询的 web 平台有关 现在根据您在浏览器中运行 html 代码的要求是一些扩展名为.html 的文件。

import webbrowser
html='<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"/><title>Sorry...</title><style> body { font-family: verdana, arial, sans-serif; background-color: #fff; color: #000; }</style></head><body><div><table><tr><td><b><font face=sans-serif size=10><font color=#4285f4>G</font><font color=#ea4335>o</font><font color=#fbbc05>o</font><font color=#4285f4>g</font><font color=#34a853>l</font><font color=#ea4335>e</font></font></b></td><td style="text-align: left; vertical-align: bottom; padding-bottom: 15px; width: 50%"><div style="border-bottom: 1px solid #dfdfdf;">Sorry...</div></td></tr></table></div><div style="margin-left: 4em;"><h1>We\'re sorry...</h1><p>... but your computer or network may be sending automated queries. To protect our users, we can\'t process your request right now.</p></div><div style="margin-left: 4em;">See <a href="https://support.google.com/websearch/answer/86640">Google Help</a> for more information.<br/><br/></div><div style="text-align: center; border-top: 1px solid #dfdfdf;"><a href="https://www.google.com">Google Home</a></div></body></html>'
with open('browser.html','w') as f:
    f.write(html)
    f.close
webbrowser.open_new_tab('browser.html')

OUTPUT

1.文件get在目录中创建

在此处输入图像描述

2. html 文件在浏览器中打开

在此处输入图像描述

暂无
暂无

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

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