python解析html文件代码示例

发布时间:2020-08-18编辑:脚本学堂
python如何解析html文件,python使用lxml、requests模块解析html格式文件。

代码:
 

复制代码 代码示例:
#!/usr/bin/env python
# coding=utf-8
 
import requests
import lxml
from lxml import html
 
r = requests.get("http://172.16.1.132:10022")
doc = html.document_fromstring(r.content)
table = doc.xpath('/html/body/table')[0]
informs = table.findall('tr/td/font')
 
vminfo = "vm_name, vm_mac, vm_state, vm_heart, eng_version, end_db_date, count".split(", ")
texts = [item.text for  item in informs]
loops = loop = len(informs)/7
vms = []
for i in range(loop):
    d = {}
    for j in range(7):
    print(vminfo[j])
        d[ vminfo[j] ] = texts[ i*7 +j]
    print( d )
    vms.append(d)
 
for i in vms:
    print(i)