python爬虫之Xpath

了解XML xpath

Xpath:http://www.w3school.com.cn/xpath/index.asp

 

安装库

Pip install lxml

什么是xpath

Xml是用来存储和传输数据使用的

html的不同有两点:

  1. html用来显示数据,xml是用来传输数据
  2. Html标签是固定的,xml标签是自定义的

Xpath用来在xml中查找指定的元素,它是一种路径表达式。

 

常用的路径表达式:

// :不考虑位置的查找

./  : 从当前节点开始往下查找

@ :选取属性

 

 

实例

‘’’

<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>

<book>

  <title lang="eng">Harry Potter</title>

  <price>29.99</price>

</book>

<book>

  <title lang="eng">Learning XML</title>

  <price>39.95</price>

</book>

</bookstore>

‘’’

Ps:

/bookstore/book  选取根节点bookstore下面所有的book (只能找儿子)

//book   选取所有的book元素,而不管它们在文档中的位置。

bookstore//book  选择属于 bookstore 元素的后代的所有 book 元素,而不管它们位于 bookstore 之下的什么位置 (还可以找孙子)

 

/bookstore/book[1]      bookstore 下的的第一个 book 元素

/bookstore/book[last()]   bookstore的最后一个 book 元素

/bookstore/book[last()-1] 选取属于 bookstore 子元素的倒数第二个 book 元素

/bookstore/book[position()<3]   选取最前面的两个属于 bookstore 元素的子元素的 book 元素。

//title[@lang]   所有带有 lang 的属性的 title 元素。

//title[@lang='eng'] 所有的lang属性为engtitle节点

 

* 匹配任何元素节点。

/bookstore/* 选取 bookstore 元素的所有子元素。

//* 选取文档中的所有元素。

/title[@*] 选取所有带有属性的 title 元素。

 

 

 

安装xpath插件

xpath插件拖动到谷歌浏览器扩展程序中,安装成功

启动和关闭插件:ctrl+shift+x

以百度首页为例子:

属性定位

//input[@id=”kw”]  这是搜索框的路径表达式

//input[@class=”bg s_btn”]  这是百度一下的路径表达式

层级定位

//div[@id=”head”]/div/div[2]/a[@class=”toindex”]

曾经加索引  这是百度一下的路径表达式

Ps:索引从1开始

//div[id=:”head”]//a[@class=”toindex”]

Ps:双斜杠代表下面所有的a节点,不管位置

逻辑运算

//input[@class=”s_ipt”and @name=”wd”]

模糊匹配

 Contains

//input[contains(@class,”s_i”)]

所有的input class属性 并且属性中带有s_i的节点

starts-with

//input[start-with(@class,”s”)]

所有的input class属性 并且属性以s开头的节点

Ps:没有endwith

 

取文本

//div[@id=”ul”/a[5]/text()]  #贴吧  获取节点内容

//div[@id=”ul”/text()]  #贴吧 获取节点内不带标签的所有内容

 

取属性

//div[@id=”ul”/a[5]/@href 获取属性herf

 

代码中操作xpath

导入库

from lxml import etree

两种方式使用:都是将html文档变成一个对象,然后调用对象的方法去查找指定的节点

(1) 本地文件

tree = etree.parse(文件名)

(2) 网络文件

tree=etree.HTML(网页字符串)

 

 

例子:

from lxml import etree

#生成对象

tree = etree.paese(“xpath.html”)

ret = tree.xpath(‘//div[@class=”tang”]/ul/li[1]/text()’) #打印出内容

Print(ret) #这里是一个列表

‘’’

另一种写法

ret = tree.xpath(‘//div[@class=”tang”]/ul/li[1]’)

print(ret[0].text)

‘’’

ret = tree.xpath(路径表达式)

ret是一个列表

 

 

from lxml import etree

tree = etree.paese(“xpath.html”)

ret = tree.xpath(‘//div[@class=”tang”]/ul/li[last()]/a/@href’)#href属性

print(ret)

 

 

from lxml import etree

tree = etree.paese(“xpath.html”)

ret = tree.xpath(‘//div[@class=”tang”]/ul/li[@class=”love”

and @name=”yang”]’)

print(ret[0].text)

 

#取出\n ,\t 换成空字符串直接获得纯文本

直接将所有的内容拼接起来

ret = tree.xpath(‘//div[@class=”song”]’)

string = ret[0].xpath(‘string(.)’)

print(string.replace(‘\n’,’’).replace(‘\t’,’’))#直接将所有的内容拼接起来

 

 

这里是

 

 

‘’’

from lxml import etree

 

text = '''

<div>

    <ul>

         <li class="item-0"><a href="link1.html">first item</a></li>

         <li class="item-1"><a href="link2.html">second item</a></li>

         <li class="item-inactive"><a href="link3.html">third item</a></li>

         <li class="item-1"><a href="link4.html">fourth item</a></li>

         <li class="item-0"><a href="link5.html">fifth item</a>

     </ul>

 </div>

'''

html = etree.HTML(text)

etree.parse()

# print(html)

print(type(html))  #  <class 'lxml.etree._Element'>

# 与之前这个类型类似 bs4.element.Tag

 

# print(html.xpath('li'))  # []

# print(html.xpath('/li'))  # []

# print(html.xpath('//li'))  # [多个element]

 

print(html.xpath('//a'))  # 取 元素当中的内容

# print(html.xpath('//a/text()'))  # 取 元素当中的内容

print(html.xpath('//a/@href'))  # 取 元素当中的属性

# print(html.xpath('//li[@class="item-0"]//text()'))

# print(html.xpath('//li[@class="item-0"]//@href'))

print(html.xpath('//li[@class="item-0"]/a/text()'))

‘’’

 

posted on 2019-03-05 10:23  我是夜雨吖  阅读(319)  评论(0编辑  收藏  举报

导航