使用Python打印HTML Progress Bar

时间:2017-05-23 07:19:27

标签: python html lxml

我正在尝试使用Python脚本将以下HTML打印到页面。我在这里关注这个例子: https://www.w3schools.com/w3css/tryit.asp?filename=tryw3css_progressbar

我无法使用以下代码:

from lxml import etree, html

document_root = html.fromstring("<!DOCTYPE html> <html> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css"> <body> <div class="w3-border"> <div class="w3-grey" style="height:24px;width:20%"> </div> </div> </body> </html>")
print(etree.tostring(document_root, encoding='unicode', pretty_print=True))

1 个答案:

答案 0 :(得分:0)

你需要逃避双引号

from lxml import etree, html    

str = '<!DOCTYPE html> <html> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css"> <body> <div class="w3-border"> <div class="w3-grey" style="height:24px;width:20%"> </div> </div> </body> </html>'
document_root = html.fromstring(str)
print(etree.tostring(document_root, encoding='unicode', pretty_print=True))

执行此脚本,

$ python test.py
<html> <head><meta name="viewport" content="width=device-width, initial-scale=1"/> <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css"/> </head><body> <div class="w3-border"> <div class="w3-grey" style="height:24px;width:20%"> </div> </div> </body> </html>

更新:

下面的命令将让你立即看到输出(我假设你在* nix机器上并安装了firefox。或启动任何其他浏览器)

$ python test.py >> index.html && python -m SimpleHTTPServer && firefox localhost:8000
Serving HTTP on 0.0.0.0 port 8000 ...
127.0.0.1 - - [23/May/2017 11:34:10] "GET / HTTP/1.1" 200 -