网络爬虫Xpath

Xpath的作用:

XPath是一门在XML文档中查找信息的语言。XML文档包括:HTML/XHTMLXML/XMLNamespaces

Xpath表达式:

XPath(全称:XML Path Language)即 XML 路径语言,它是一门在 XML 文档中查找信息的语言,最初被用来搜寻 XML 文档,同时它也适用于搜索 HTML 文档。因此,在爬虫过程中可以使用 XPath 来提取相应的数据。

提示:XML 是一种遵守 W3C 标椎的标记语言,类似于 HTML,但两者的设计目的是不同,XML 通常被用来传输和存储数据,而 HTML 常用来显示数据。

您可以将 Xpath 理解为在XML/HTML文档中检索、匹配元素节点的工具。

Xpath 使用路径表达式来选取XML/HTML文档中的节点或者节点集。Xpath 的功能十分强大,它除了提供了简洁的路径表达式外,还提供了100 多个内建函数,包括了处理字符串、数值、日期以及时间的函数。因此 Xpath 路径表达式几乎可以匹配所有的元素节点。

XPath的节点:

请看下面这个XML文档:

<?xmlversion="1.0"encoding="UTF-8"?>
<bookstore><book><titlelang="en">HarryPotter</title>
<author>JK.Rowling</author>
<year>2005</year>
<price>29.99</price>
</book></bookstore>

上面的XML文档中的节点例子:

<bookstore>(文档节点)
<author>JK.Rowling</author>
(元素节点)lang="en"(属性节点)

节点关系

父(Parent)

每个元素以及属性都有一个父。在下面的例子中,book元素是title、author、year以及price元素的父

<book><title>HarryPotter</title>
<author>JK.Rowling</author>
<year>2005</year><price>29.99</price></book>

子(Children)

元素节点可有零个、一个或多个子。在下面的例子中,title、author、year以及price元素都是book元素的子:

<book><title>HarryPotter</title>
<author>JK.Rowling</author>
<year>2005</year>
<price>29.99</price></book>

同胞(Sibling)

拥有相同的父的节点在下面的例子中,title、author、year以及price元素都是同胞:

<book><title>HarryPotter</title>
<author>JK.Rowling</author>
<year>2005</year><price>29.99</price></book>

先辈(Ancestor)

某节点的父、父的父,等等。在下面的例子中,title元素的先辈是book元素和bookstore元素:

<bookstore>
<book><title>HarryPotter</title>
<author>JK.Rowling</author>
<year>2005</year><price>29.99</price>
</book></bookstore>

 后代(Descendant)

某个节点的子,子的子,等等。在下面的例子中,bookstore的后代是book、title、author、year以及price元素:

<bookstore><book><title>HarryPotter</title>
<author>JK.Rowling</author>
<year>2005</year><price>29.99</price>
</book></bookstore>

etree.HTML()


etree.HTML()可以用来解析字符串格式的HTML文档对象,将传进去的字符串转变成_Element对象。作为_Element对象,可以方便的使用getparent()、remove()、xpath()等方法。并且自动补全html代码方便更好操作和查找。

from lxml import etree
s="""	<div class="ti">
			<h3>你和李维宁相同技能:</h3>
			<div class="option">
				<input type="radio" name="r2" value="true"/>
				<label>反向操作</label>
			</div>
			<div class="option">
				<input type="radio" name="r2" />
				<label>精准卡点</label>
			</div>
			<div class="option">
				<input type="radio" name="r2"/>
				<label>和孙哥撞撞</label>
			</div>
			<div   class="jie">
				<div>解析: <span hidden="hidden">反向操作</span></div>
			</div>
		</div>"""
#将字符串转换为html
html=etree.HTML(s)
print(html)
html_string=etree.tostring(html)
print(html_string.decode('utf-8'))

/代表根节点

data = html.xpath('/html')
print(data)

//代表后面的节点可以在任意位置

data2 = html.xpath('//a')
for i in data2:
    if hasattr(i, 'text'):
        print(i.text)
    else:
        print(i)

具体路径

data3 = html.xpath('/html/body//div/label')
for i in data3:
    print(i)

.代表当前节点 ..代表上级节点

data4 = html.xpath('/html/body/div')
for i in data4:
    li = i.xpath('./h3')
    for j in li:
        a = j.xpath('./label')
        for k in a:
            print(k.text)
print("*" * 100)
for i in data4:
    ul = i.xpath('../label')
    print(ul)

@选取属性

data4=html.xpath("//input/@type")
print(data4)
data4=html.xpath("//a/@href")
print(data4)

谓语,指定具体的节点

data4=html.xpath("//div[1]/label")
print(data4)
data4=html.xpath("//div[last()]")
print(data4)
data4=html.xpath('//span[@hidden="hidden"]')
for i in data4:
    if hasattr(i,'text'):
        print(i.text)

*未知节点

data4=html.xpath("//*")
print(len(data4))
for i in data4:
    if hasattr(i, 'text'):
        print(i.text)
data4=html.xpath("//@*")
print(len(data4))
print(data4)

| 匹配两个节点

data4=html.xpath("//li | //a")
print(len(data4))

案例

from lxml import etree
#解析文件:html文件,但是自定义解析器,因为etree默认是xml解析器
#自定义解释器,
parser=etree.HTMLParser(encoding='utf-8')
#解析html文件为html对象
html=etree.parse("index.html",parser=parser)
 
# #将对象html转换字符串,解码,打印
html=etree.tostring(html,pretty_print=True)
res=html.decode("utf-8")
print(res)
 
 
#打印内容。[]表示带有title属性的a
data=html.xpath('//a[@title]')
for i in data:
    #. 当前节点a ,寻找title属性值
    title=i.xpath('./@title')
    #寻找当前节点文本
    desc=i.xpath('./text()')
    print(title,desc)
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值