从Python启动HTML code在浏览器(由BeautifulSoup生成)直 [英] Launch HTML code in browser (that is generated by BeautifulSoup) straight from Python

查看:187
本文介绍了从Python启动HTML code在浏览器(由BeautifulSoup生成)直的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用BeautifulSoup的Python 3.3,成功拉从网页所需的信息。我也用BeautifulSoup产生新的HTML code,以显示这个信息。目前,我的Python程序打印出HTML code,然后我必须复制,粘贴,保存为HTML文件,然后从那里,我可以在浏览器中测试它。

I have used BeautifulSoup for Python 3.3 to successfully pull desired info from a web page. I have also used BeautifulSoup to generate new HTML code to display this info. Currently, my Python program prints out the HTML code, which I then have to copy, paste, and save as an HTML file, then from there, I can test it in a browser.

所以我的问题是,有没有在Python的方式来推出由BeautifulSoup生成的HTML code在Web浏览器,以便我不必经过我现在使用的复制和粘贴的方法?

So my question is this, is there a way in Python to launch the HTML code generated by BeautifulSoup in a web browser so that I don't have to go through the copy and paste method I use now?

推荐答案

使用<一个href=\"http://docs.python.org/3/library/webbrowser.html#webbrowser.open\"><$c$c>webbrowser.open:

import os
import webbrowser

html = '<html> ...  generated html string ...</html>'
path = os.path.abspath('temp.html')
url = 'file://' + path

with open(path, 'w') as f:
    f.write(html)
webbrowser.open(url)

这篇关于从Python启动HTML code在浏览器(由BeautifulSoup生成)直的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆