Joh*_*nyW 19 html python beautifulsoup python-3.x
我使用BeautifulSoup for Python 3.3成功从网页中提取所需的信息.我还使用BeautifulSoup生成新的HTML代码来显示此信息.目前,我的Python程序打印出HTML代码,然后我必须将其复制,粘贴并保存为HTML文件,然后从那里,我可以在浏览器中测试它.
所以我的问题是,在Python中是否有办法在Web浏览器中启动BeautifulSoup生成的HTML代码,这样我就不必经历我现在使用的复制和粘贴方法?
fal*_*tru 22
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)
Run Code Online (Sandbox Code Playgroud)