1. from lxml import etree
  2. 2 text = '''
  3. 3 <div>
  4. 4 <ul>
  5. 5 <li class = "item-0"><a herf = "link1.html">first item</a></li>
  6. 6 <li class = "item-1"><a herf = "link2.html">second item</a></li>
  7. 7 <li class = "item-inactive"><a herf = "link3.html">third item</a></li>
  8. 8 <li class = "item-1"><a herf = "link4.html">fourth item</a></li>
  9. 9 <li class = "item-0"><a herf = "link5.html">fifth item</a></li>
  10. 10 </ul>
  11. 11 </div>
  12. 12 '''
  13. 13 html = etree.HTML(text)
  14. 14 result = etree.tostring((html))#输出修正后的HTML文本
  15. 15 code_all = html.xpath("//*")#选取HTML全部的节点
  16. 16 code_li = html.xpath("//li")
  17. 17 code_a = html.xpath("//li/a")#选取HTMLli节点的子节点a
  18. 18 code_p = html.xpath("//a[@herf = 'link4.html']/../@class")#一直子节点寻找父节点的class属性
  19. 19 print(code_p)
  20. 20 print(code_li)
  21. 21 print("///")
  22. 22 print(code_all)
  23. 23 print("///")
  24. 24 print(code_a)
  25. 25 #属性匹配
  26. 26 attribute = html.xpath("//li[@class = 'item-0']")
  27. 27 print(attribute)
  28. 28 #文本获取
  29. 29 text = html.xpath("//li/text()")
  30. 30 print(text)
  31. 31 #属性获取
  32. 32 attribute_get = html.xpath("//li/a/@herf")
  33. 33 print(attribute_get)
  34. 34 #属性多值匹配
  35. 35 text1 = """
  36. 36 <li class = "li li-fist"><a href = "link.html">first item</a></li>
  37. 37 """
  38. 38 html1 = etree.HTML(text1)
  39. 39 attribute_number = html1.xpath("//li[contains(@class,'li')]/a/text()")
  40. 40 print(attribute_number)
  41. 41 #多属性匹配
  42. 42 text2 = """
  43. 43 <li calss = "li li-first" name = "name"><a href = "link.html">first item</a></li>
  44. 44 """
  45. 45 html2 = etree.HTML(text2)
  46. 46 attribute_text2 = html2.xpath("//li[contains(@calss,'li') and @name = 'name']/a/text()")
  47. 47 print(attribute_text2)
  48. 48 #按序选择
  49. 49 """
  50. 50 有时候,我们在选择的时候某些属性可能同时匹配了多个节点,但是只想要其中某个节点
  51. 51 这是可以利用中括号传入索引的方法获取特定次序的节点
  52. 52 """
  53. 53 text3 = '''
  54. 54 <div>
  55. 55 <ul>
  56. <li class = "item-0"><a herf = "link1.html">first item</a></li>
  57. 57 <li class = "item-1"><a herf = "link2.html">second item</a></li>
  58. 58 <li class = "item-inactive"><a herf = "link3.html">third item</a></li>
  59. 59 <li class = "item-1"><a herf = "link4.html">fourth item</a></li>
  60. 60 <li class = "item-0"><a herf = "link5.html">fifth item</a></li>
  61. 61 </ul>
  62. 62 </div>
  63. 63 '''
  64. 64 html3 = etree.HTML(text3)
  65. 65 result = html3.xpath("//li[1]/a/text()")#选取第一个li节点
  66. 66 print(result)
  67. 67 result = html3.xpath("//li[last()]/a/text()")#选取左后一个li节点
  68. 68 print(result)
  69. 69 result = html3.xpath("//li[position() < 3]/a/text()")#选取位置小于三的节点
  70. 70 print(result)
  71. 71 #节点轴选取
  72. 72 result = html3.xpath("//li[1]/ancestor::*")#获取所有祖先节点,后跟*表示匹配所有节点
  73. 73 print(result)
  74. 74 result = html3.xpath("//li[1]/ancestor::div")#获取div这个祖先节点
  75. 75 print(result)
  76. 76 result = html3.xpath("//li[1]/attribute::*")#获取所有属性
  77. 77 print(result)
  78. #运行结果
  79. ['item-1']
  80. [<Element li at 0x7f72f489c888>, <Element li at 0x7f72f489c948>, <Element li at 0x7f72f489c9c8>, <Element li at 0x7f72f489ca08>, <Element li at 0x7f72f489ca88>]
  81. ///
  82. [<Element html at 0x7f72f489c808>, <Element body at 0x7f72f489c788>, <Element div at 0x7f72f489c748>, <Element ul at 0x7f72f489c848>, <Element li at 0x7f72f489c888>, <Element a at 0x7f72f489c908>, <Element li at 0x7f72f489c948>, <Element a at 0x7f72f489c988>, <Element li at 0x7f72f489c9c8>, <Element a at 0x7f72f489c8c8>, <Element li at 0x7f72f489ca08>, <Element a at 0x7f72f489ca48>, <Element li at 0x7f72f489ca88>, <Element a at 0x7f72f489cac8>]
  83. ///
  84. [<Element a at 0x7f72f489c908>, <Element a at 0x7f72f489c988>, <Element a at 0x7f72f489c8c8>, <Element a at 0x7f72f489ca48>, <Element a at 0x7f72f489cac8>]
  85. [<Element li at 0x7f72f489c888>, <Element li at 0x7f72f489ca88>]
  86. []
  87. ['link1.html', 'link2.html', 'link3.html', 'link4.html', 'link5.html']
  88. ['first item']
  89. ['first item']
  90. ['first item']
  91. ['fifth item']
  92. ['first item', 'second item']
  93. [<Element html at 0x7f72f489cdc8>, <Element body at 0x7f72f489cec8>, <Element div at 0x7f72f489cf48>, <Element ul at 0x7f72f489cf08>]
  94. [<Element div at 0x7f72f489cf48>]
  95. ['item-0']

解析库--XPath的更多相关文章

  1. 网页解析库-Xpath语法

    网页解析库 简介 除了正则表达式外,还有其他方便快捷的页面解析工具 如:lxml (xpath语法) bs4 pyquery等 Xpath 全称XML Path Language, 即XML路径语言, ...

  2. Python3编写网络爬虫05-基本解析库XPath的使用

    一.XPath 全称 XML Path Language 是一门在XML文档中 查找信息的语言 最初是用来搜寻XML文档的 但是它同样适用于HTML文档的搜索 XPath 的选择功能十分强大,它提供了 ...

  3. python爬虫基础04-网页解析库xpath

    更简单高效的HTML数据提取-Xpath 本文地址:https://www.jianshu.com/p/90e4b83575e2 XPath 是一门在 XML 文档中查找信息的语言.XPath 用于在 ...

  4. 爬虫之解析库Xpath

    简介 XPath即为XML路径语言(XML Path Language),它是一种用来确定XML文档中某部分位置的语言. XPath基于XML的树状结构,提供在数据结构树中找寻节点的能力.起初XPat ...

  5. 爬虫解析库xpath

    # xpath简介 XPath即为XML路径语言(XML Path Language),它是一种用来确定XML文档中某部分位置的语言.用于在 XML 文档中通过元素和属性进行导航. XPath基于XM ...

  6. BeautifulSoup与Xpath解析库总结

    一.BeautifulSoup解析库 1.快速开始 html_doc = """ <html><head><title>The Dor ...

  7. (最全)Xpath、Beautiful Soup、Pyquery三种解析库解析html 功能概括

    一.Xpath 解析   xpath:是一种在XMl.html文档中查找信息的语言,利用了lxml库对HTML解析获取数据. Xpath常用规则: nodename :选取此节点的所有子节点 // : ...

  8. 网络爬虫之Selenium模块和Xpath表达式+Lxml解析库的使用

    实际生产环境下,我们一般使用lxml的xpath来解析出我们想要的数据,本篇博客将重点整理Selenium和Xpath表达式,关于CSS选择器,将另外再整理一篇! 一.介绍: selenium最初是一 ...

  9. xpath beautiful pyquery三种解析库

    这两天看了一下python常用的三种解析库,写篇随笔,整理一下思路.太菜了,若有错误的地方,欢迎大家随时指正.......(conme on.......) 爬取网页数据一般会经过 获取信息-> ...

随机推荐

  1. HDU - 4722 Good Numbers 【找规律 or 数位dp模板】

    If we sum up every digit of a number and the result can be exactly divided by 10, we say this number ...

  2. C++ STL (基础)

    STL是什么(STL简介) 本节主要讲述 STL 历史.STL 组件.STL 基本结构以及 STL 编程概述.STL 历史可以追溯到 1972 年 C 语言在 UNIX 计算机上的首次使用.直到 19 ...

  3. Linux 应用开发----socket编程笔记

    Linux socket编程 套接字定义描述 套接字的域 AF_INET ====>IPv4 AF_INET6 ====>IPv6 AF_UNIX ====>unix 域 AF_UP ...

  4. C#通过NI-VISA操作Tektronix TBS 2000B系列示波器

    一.概述 本文描述采用C#语言访问控制Tektronix TBS 2000B 系列示波器.接口协议采用NI-VISA. 最近一个项目需要和一款示波器进行通信,需要对示波器进行一些简单控制并获取到波形数 ...

  5. Vue & Sentry sourcemaps All In One

    Vue & Sentry sourcemaps All In One vue & sentry & sourcemaps https://docs.sentry.io/plat ...

  6. macOS & pbcopy

    macOS & pbcopy copy from command line pbcopy $ whoami | pbcopy # xgqfrms-mbp $ echo "hello, ...

  7. 如何获取豆瓣电影 API Key

    如何获取豆瓣电影 API Key 豆瓣 API Key 不能使用了 ! solutions & !== ? https://frodo.douban.com/api/v2/subject_co ...

  8. Google & Chrome console & text adventure game

    Google & Chrome console & text adventure game Google's text adventure game https://www.googl ...

  9. Python module all in one

    Python module all in one Python Modules https://docs.python.org/3/tutorial/modules.html Fibonacc # F ...

  10. UI & APP

    UI & APP lanhu http://help.lanhuapp.com/hc/ http://help.lanhuapp.com/hc/kb/article/1173434/ 快速使用 ...