python

超轻量级php框架startmvc

Python基于lxml模块解析html获取页面内所有叶子节点xpath路径功能示例

更新时间:2020-06-02 10:00:01 作者:startmvc
本文实例讲述了Python基于lxml模块解析html获取页面内所有叶子节点xpath路径功能。分享给大

本文实例讲述了Python基于lxml模块解析html获取页面内所有叶子节点xpath路径功能。分享给大家供大家参考,具体如下:

因为需要使用叶子节点的路径来作为特征,但是原始的lxml模块解析之后得到的却是整个页面中所有节点的xpath路径,不是我们真正想要的形式,所以就要进行相关的处理才行了,差了很多网上的博客和文档也没有找到一个是关于输出html中全部叶子节点的API接口或者函数,也可能是自己没有那份耐心,没有找到合适的资源,只好放弃了寻找,但是这并不说明没有其他的方法了,在对页面全部节点的xpath输出之后观察得到的结果就是:

1.路径之间存在包含性

2.叶子节点的路径必然包含上一个叶子节点路径到下一个叶子节点路径之间的路径

3.所有的叶子节点均不存在包含性

基于这些观察就可以实践了,我采用的方法是,设置一个标志位,如果当前路径被下一条路径包含的话就把下标加1处理,直到遇上不包含的情况是这一条路径就是一条叶子节点路径,加入结果列表即可,经检验和原始路径列表对比后,结果路径列表均不存在包含性,暂可认为均为叶子节点的路径,不敢确定的说是因为:竟没有大量做实验观察是否有另类,不过方法是可行的,下面是具体实现:


#!usr/bin/env python
#encoding:utf-8
'''''
__author__:沂水寒城
功能:得到页面的叶子节点的xpath
'''
def get_leaf_node_xpath(one_page_xpath):
 '''''
 输入:一个页面的原始xpath路径列表
 输出:只包含页面的叶子节点的xpath列表
 '''
 one_page_xpath.append('0')
 leaf_node_xpath_list=[]
 for i in range(len(one_page_xpath)-1):
 j=i+1
 one_xpath=one_page_xpath[i]
 two_xpath=one_page_xpath[j]
 if one_xpath in two_xpath:
 one_xpath=two_xpath
 two_xpath=one_page_xpath[j+1]
 else:
 leaf_node_xpath_list.append(one_xpath)
 return leaf_node_xpath_list
if __name__ == '__main__':
 with open('baidu.txt') as f:
 html=f.read()
 htree, one_page_xpath=get_clean_allnodes_xpath(html)
 leaf_node_xpath_list=get_leaf_node_xpath(one_page_xpath)
 print leaf_node_xpath_list
 with open('data/dom_leaf_xpath/original_xpath.txt', 'w') as f1:
 for one_line in one_page_xpath:
 f1.write(one_line.strip()+'\n')
 with open('data/dom_leaf_xpath/leaf_xpath.txt', 'w') as f2:
 for one_line in leaf_node_xpath_list:
 f2.write(one_line.strip()+'\n')

打开保存的文件内容为:

original_xpath.txt内容为:

/html /html/head /html/head/meta[1] /html/head/meta[2] /html/head/meta[3] /html/head/meta[4] /html/head/title /html/body /html/body/p /html/body/p/comment()[1] /html/body/p/comment()[2] /html/body/p/comment()[3] /html/body/p/meta /html/body/div[1] /html/body/div[1]/div[1] /html/body/div[1]/div[1]/div /html/body/div[1]/div[1]/div/div[1] /html/body/div[1]/div[1]/div/div[1]/div /html/body/div[1]/div[1]/div/div[1]/div/div[1] /html/body/div[1]/div[1]/div/div[1]/div/a /html/body/div[1]/div[1]/div/div[1]/div/form /html/body/div[1]/div[1]/div/div[1]/div/form/span[1] /html/body/div[1]/div[1]/div/div[1]/div/form/span[2] /html/body/div[1]/div[1]/div/div[1]/div/form/span[3] /html/body/div[1]/div[1]/div/div[1]/div/form/span[3]/span /html/body/div[1]/div[1]/div/div[1]/div/form/span[3]/span/div /html/body/div[1]/div[1]/div/div[1]/div/form/span[3]/span/div/span /html/body/div[1]/div[1]/div/div[1]/div/form/span[3]/span/ul /html/body/div[1]/div[1]/div/div[1]/div/form/span[3]/span/ul/li[1] /html/body/div[1]/div[1]/div/div[1]/div/form/span[3]/span/ul/li[1]/a /html/body/div[1]/div[1]/div/div[1]/div/form/span[3]/span/ul/li[2] /html/body/div[1]/div[1]/div/div[1]/div/form/span[3]/span/ul/li[2]/a /html/body/div[1]/div[1]/div/div[1]/div/form/span[3]/span/ul/li[3] /html/body/div[1]/div[1]/div/div[1]/div/form/span[3]/span/ul/li[4] /html/body/div[1]/div[1]/div/div[1]/div/form/span[3]/span/ul/li[4]/a /html/body/div[1]/div[1]/div/div[1]/div/div[2] /html/body/div[1]/div[1]/div/div[2] /html/body/div[1]/div[1]/div/div[2]/a[1] /html/body/div[1]/div[1]/div/div[2]/a[2] /html/body/div[1]/div[1]/div/div[2]/a[3] /html/body/div[1]/div[1]/div/div[3] /html/body/div[1]/div[1]/div/div[3]/a[1] /html/body/div[1]/div[1]/div/div[3]/a[2] /html/body/div[1]/div[1]/div/div[3]/a[3] /html/body/div[1]/div[1]/div/div[3]/a[4] /html/body/div[1]/div[1]/div/div[3]/a[5] /html/body/div[1]/div[1]/div/div[3]/a[6] /html/body/div[1]/div[1]/div/div[3]/a[7] /html/body/div[1]/div[1]/div/div[3]/a[8] /html/body/div[1]/div[1]/div/div[3]/a[9] /html/body/div[1]/div[2] /html/body/div[1]/div[2]/a[1] /html/body/div[1]/div[2]/a[2] /html/body/div[1]/div[2]/a[3] /html/body/div[1]/div[2]/a[4] /html/body/div[1]/div[2]/a[5] /html/body/div[1]/div[2]/a[6] /html/body/div[1]/div[2]/a[7] /html/body/div[1]/div[2]/a[8] /html/body/div[1]/div[2]/a[9] /html/body/div[1]/div[3] /html/body/div[1]/div[3]/div /html/body/div[1]/div[3]/div/div /html/body/div[1]/div[3]/div/div/div[1] /html/body/div[1]/div[3]/div/div/div[2] /html/body/div[1]/div[3]/div/div/div[2]/p /html/body/div[1]/div[4] /html/body/div[1]/div[4]/div /html/body/div[1]/div[4]/div/div /html/body/div[1]/div[4]/div/div/p[1] /html/body/div[1]/div[4]/div/div/p[1]/a[1] /html/body/div[1]/div[4]/div/div/p[1]/a[2] /html/body/div[1]/div[4]/div/div/p[1]/a[3] /html/body/div[1]/div[4]/div/div/p[1]/a[4] /html/body/div[1]/div[4]/div/div/p[2] /html/body/div[1]/div[4]/div/div/p[2]/a[1] /html/body/div[1]/div[4]/div/div/p[2]/a[2] /html/body/div[1]/div[4]/div/div/p[2]/a[3] /html/body/div[1]/div[5] /html/body/div[2] /html/body/div[3] /html/body/div[4] 0

最后一行的0只是为了代码中列表能够遍历到最后一个路径而不出现索引错误的一个简单的处理,人为的添加了一个元素,没有实际意义,也不会参与处理

leaf_xpath.txt内容为:

/html/head/meta[1] /html/head/meta[2] /html/head/meta[3] /html/head/meta[4] /html/head/title /html/body/p/comment()[1] /html/body/p/comment()[2] /html/body/p/comment()[3] /html/body/p/meta /html/body/div[1]/div[1]/div/div[1]/div/div[1] /html/body/div[1]/div[1]/div/div[1]/div/a /html/body/div[1]/div[1]/div/div[1]/div/form/span[1] /html/body/div[1]/div[1]/div/div[1]/div/form/span[2] /html/body/div[1]/div[1]/div/div[1]/div/form/span[3]/span/div/span /html/body/div[1]/div[1]/div/div[1]/div/form/span[3]/span/ul/li[1]/a /html/body/div[1]/div[1]/div/div[1]/div/form/span[3]/span/ul/li[2]/a /html/body/div[1]/div[1]/div/div[1]/div/form/span[3]/span/ul/li[3] /html/body/div[1]/div[1]/div/div[1]/div/form/span[3]/span/ul/li[4]/a /html/body/div[1]/div[1]/div/div[1]/div/div[2] /html/body/div[1]/div[1]/div/div[2]/a[1] /html/body/div[1]/div[1]/div/div[2]/a[2] /html/body/div[1]/div[1]/div/div[2]/a[3] /html/body/div[1]/div[1]/div/div[3]/a[1] /html/body/div[1]/div[1]/div/div[3]/a[2] /html/body/div[1]/div[1]/div/div[3]/a[3] /html/body/div[1]/div[1]/div/div[3]/a[4] /html/body/div[1]/div[1]/div/div[3]/a[5] /html/body/div[1]/div[1]/div/div[3]/a[6] /html/body/div[1]/div[1]/div/div[3]/a[7] /html/body/div[1]/div[1]/div/div[3]/a[8] /html/body/div[1]/div[1]/div/div[3]/a[9] /html/body/div[1]/div[2]/a[1] /html/body/div[1]/div[2]/a[2] /html/body/div[1]/div[2]/a[3] /html/body/div[1]/div[2]/a[4] /html/body/div[1]/div[2]/a[5] /html/body/div[1]/div[2]/a[6] /html/body/div[1]/div[2]/a[7] /html/body/div[1]/div[2]/a[8] /html/body/div[1]/div[2]/a[9] /html/body/div[1]/div[3]/div/div/div[1] /html/body/div[1]/div[3]/div/div/div[2]/p /html/body/div[1]/div[4]/div/div/p[1]/a[1] /html/body/div[1]/div[4]/div/div/p[1]/a[2] /html/body/div[1]/div[4]/div/div/p[1]/a[3] /html/body/div[1]/div[4]/div/div/p[1]/a[4] /html/body/div[1]/div[4]/div/div/p[2]/a[1] /html/body/div[1]/div[4]/div/div/p[2]/a[2] /html/body/div[1]/div[4]/div/div/p[2]/a[3] /html/body/div[1]/div[5] /html/body/div[2] /html/body/div[3] /html/body/div[4]

针对这个问题暂时可以想到的方法就是这样的,利用包含性来解决,没有其他的新思路,如果还有新的思路欢迎交流学习!

PS:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:

在线XML/JSON互相转换工具: http://tools.jb51.net/code/xmljson

在线格式化XML/在线压缩XMLhttp://tools.jb51.net/code/xmlformat

XML在线压缩/格式化工具: http://tools.jb51.net/code/xml_format_compress

XML代码在线格式化美化工具: http://tools.jb51.net/code/xmlcodeformat

Python lxml模块 解析 html 页面 叶子节点 xpath路径