我试图匹配来自 html 输出的一些数据,但我不确定我能做些什么来正确执行它。因此,我使用以下代码块来提取访问和组信息的内容:
import requests
import lxml.etree as LE
import lxml.html as LH
url = "http://theurl"
r = requests.get(url,auth=('user', 'pass'))
html = r.text
root = LH.fromstring(html)
LE.strip_tags(root, 'b')
data_list = root.xpath("""//td[text()='grouplist']
/following-sibling::*""")[0]
accessList= data_list.xpath("""//td[text()='access']
/following-sibling::*/text()""")
groups = data_list.xpath("""//td[text()='groups']
/following-sibling::*/text()""")
如果我打印 accessList,我有我想要的数据:
print accessList
['Administrators', 'group_a', 'group_b', 'group_c']
但是当我打印组时,返回的结果将是:
print groups:
['\n','\n','\n']
有了这些信息,可以做些什么来获得:
print groups
['group_a', 'group_b', 'group_c']
在这里,您可以看到返回的 html 结果
<TABLE bgcolor="#dddddd" border="1" />
<TR>
<TD valign="top"><B>grouplist</B></TD>
<TD>
<TABLE />
<TR>
<TD>
<TABLE bgcolor="#dddddd" border="1" />
<TR>
<TD valign="top"><B>access</B></TD>
<TD>Administrators</TD>
</TR>
<TR>
<TD valign="top"><B>inUse</B></TD>
<TD>true</TD>
</TR>
<TR>
<TD valign="top"><B>groups</B></TD>
<TD>
<TABLE>
<TR>
<TD>group_a</TD>
</TR>
<TR>
<TD>group_b</TD>
</TR>
<TR>
<TD>group_c</TD>
</TR>
</TABLE>
</TD>
</TR>
<TR>
<TD valign="top"><B>deny</B></TD>
<TD>
<TABLE>
</TABLE>
</TD>
</TR>
编辑:可以在这里测试 HTML 代码:html tester
提前致谢。