XPath 用法和实例
来源:互联网 发布:剑灵天族捏脸数据免费 编辑:程序博客网 时间:2024/06/06 03:18
Usage_XPath
#!/usr/bin/env python# -*- coding: utf-8 -*-# @Date : 2017-09-24 20:51:15# @Author : kangvcar (kangvcar@126.com)# @Link : http://www.github.com/kangvcar/# @Version : $Id$## 安装# pip install lxml## 路径表达式#表达式描述#nodename选取此节点的所有子节点。#/从根节点选取。#//从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置。#.选取当前节点。#..选取当前节点的父节点。#@选取属性from lxml import etreetext = ''''''# html = etree.HTML(text)# result = etree.tostring(html)# print result# html = etree.parse('hello.html')# result = etree.tostring(html, pretty_print=True)# print result# html = etree.HTML(text)# result = html.xpath('//li')# print result# print len(result)# print type(result)# print type(result[0])# html = etree.HTML(text)# result = html.xpath('//li/@class')# print result# html = etree.HTML(text)# result = html.xpath('//li/a[@href="link1.html"]')# print result# html = etree.HTML(text)# result = html.xpath('//li//span')# print result# html = etree.HTML(text)# result = html.xpath('//li/a//@class')# print result# html = etree.HTML(text)# result = html.xpath('//li[last()]/a/@href')# print result# html = etree.HTML(text)# result = html.xpath('//li[last()-1]/a')# print result[0].text# html = etree.HTML(text)# result = html.xpath('//*[@class="bold"]')# print result[0].text
- first item
- second item
- third item
- fourth item
- fifth item
阅读全文
0 0
- XPath 用法和实例
- Scrapy-xpath用法以及实例
- JDOM中XPath.selectNodes()和XPath.selectSingNodes()用法
- XPath实例
- XPath 实例
- XPath 实例
- XPath 实例
- Xpath 实例
- dom4j 和w3c.Dom中XPath用法
- Xpath用法
- xPath用法
- 实例简析XPath串函数和XSLT(收藏)
- 实例简析XPath串函数和XSLT
- xpath定位中starts-with、contains和text()的用法
- XPath实例演示
- XPath实例详解
- XPath Cookbook- 实例练习
- xpath实例解析
- MV3D -- Experiment
- sqldeveloper中Excel数据的导入与导出
- 1小时掌握SVN版本控制系统
- 安装lsr 在centos 6.5
- 识别浮点常量问题
- XPath 用法和实例
- 与jvm相关的部分内容
- 网络基础简介
- 构造函数的继承
- 开始我的博客人生
- ajax readyState 5种状态
- mlock实现原理及应用
- Zookeeper windows 命令行操作
- 浅谈Web网站架构演变过程及各阶段所用的技术和架构设计