码迷,mamicode.com
首页 > 其他好文 > 详细

xpath解析基础

时间:2020-07-28 10:13:54      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:usr   pat   python   parse   port   imp   img   ==   html   

#!/usr/bin/python

from lxml import etree 

if __name__ == "__main__":
    #实例化一个etree对象,且将被解析的源码加载到了该对象中
    tree = etree.parse(‘test.html‘)

    r = tree.xpath(‘./html/body/div‘)
    r = tree.xpath(‘./html//div‘)
    r = tree.xpath(‘//div‘)

    tree.xpath(‘//div[@class="song"]‘)
    tree.xpath(‘//div[@class="song"]/p[3]‘)
    tree.xpath(‘//div[@class="song"]//li[5]/a/text()‘)[0]
    tree.xpath(‘.//li[7]//text()‘)
    tree.xpath(‘//div[@class="song"]/img/@src‘)






xpath解析基础

标签:usr   pat   python   parse   port   imp   img   ==   html   

原文地址:https://www.cnblogs.com/gerenboke/p/13389022.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!