您好,登錄后才能下訂單哦!
介紹:
最近在學(xué)Python爬蟲,在這里對(duì)數(shù)據(jù)解析模塊lxml做個(gè)學(xué)習(xí)筆記。
lxml、xpath及解析器介紹:
lxml是Python的一個(gè)解析庫,支持HTML和XML的解析,支持xpath解析方式,而且解析效率非常高。xpath,全稱XML Path Language,即XML路徑語言,它是一門在XML文檔中查找信息的語言,它最初是用來搜尋XML文檔的,但是它同樣適用于HTML文檔的搜索
xml文件/html文件結(jié)點(diǎn)關(guān)系:
父節(jié)點(diǎn)(Parent)
子節(jié)點(diǎn)(Children)
同胞節(jié)點(diǎn)(Sibling)
先輩節(jié)點(diǎn)(Ancestor)
后代節(jié)點(diǎn)(Descendant)
xpath語法:
nodename 選取此節(jié)點(diǎn)的所有子節(jié)點(diǎn)
// 從任意子節(jié)點(diǎn)中選取
/ 從根節(jié)點(diǎn)選取
. 選取當(dāng)前節(jié)點(diǎn)
.. 選取當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn)
@ 選取屬性
解析器比較:
解析器 速度 難度
re 最快 難
BeautifulSoup 慢 非常簡單
lxml 快 簡單
學(xué)習(xí)筆記:
# -*- coding: utf-8 -*-
from lxml import etree
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p><b>The Dormouse's story</b></p>
<p>Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class=... ... ... ... ... ... "sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" id="link2">Lacie</a> and
<a href="http://example.com/tillie" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p>...</p>
"""
selector = etree.HTML(html_doc) #創(chuàng)建一個(gè)對(duì)象
links = selector.xpath('//p[@class="story"]/a/@href') # 取出頁面內(nèi)所有的鏈接
for link in links:
print link
xml_test = """
<?xml version='1.0'?>
<?xml-stylesheet type="text/css" href="first.css"?>
<notebook>
<user id="1" category='cb' class="dba python linux">
<name>lizibin</name>
<sex>m</sex>
<address>sjz</address>
<age>28</age>
<concat>
<email>konigerwin@163.com</email>
<phone>135......</phone>
</concat>
</user>
<user id="2" category='za'>
<name>wsq</name>
<sex>f</sex>
<address>shanghai</address>
<age>25</age>
<concat>
<email>konigerwiner@163.com</email>
<phone>135......</phone>
</concat>
</user>
<user id="3" category='za'>
<name>liqian</name>
<sex>f</sex>
<address>SH</address>
<age>28</age>
<concat>
<email>konigerwinarry@163.com</email>
<phone>135......</phone>
</concat>
</user>
<user id="4" category='cb'>
<name>qiangli</name>
<sex>f</sex>
<address>SH</address>
<age>29</age>
<concat>
<email>konigerwinarry@163.com</email>
<phone>135......</phone>
</concat>
</user>
<user id="5" class="dba linux c java python test teacher">
<name>buzhidao</name>
<sex>f</sex>
<address>SH</address>
<age>999</age>
<concat>
<email>konigerwinarry@163.com</email>
<phone>135......</phone>
</concat>
</user>
</notebook>
"""
#r = requests.get('http://xxx.com/abc.xml') 也可以請(qǐng)求遠(yuǎn)程服務(wù)器上的xml文件
#etree.HTML(r.text.encode('utf-8'))
xml_code = etree.HTML(xml_test) #生成一個(gè)etree對(duì)象
#選取所有子節(jié)點(diǎn)的name(地址)
print xml_code.xpath('//name')
選取所有子節(jié)點(diǎn)的name值(數(shù)據(jù))
print xml_code.xpath('//name/text()')
print ''
#以notebook以根節(jié)點(diǎn)選取所有數(shù)據(jù)
notebook = xml_code.xpath('//notebook')
#取出第一個(gè)節(jié)點(diǎn)的name值(數(shù)據(jù))
print notebook[0].xpath('.//name/text()')[0]
addres = notebook[0].xpath('.//name')[0]
#取出和第一個(gè)節(jié)點(diǎn)同級(jí)的 address 值
print addres.xpath('../address/text()')
#選取屬性值
print addres.xpath('../address/@lang')
#選取notebook下第一個(gè)user的name屬性
print xml_code.xpath('//notebook/user[1]/name/text()')
#選取notebook下最后一個(gè)user的name屬性
print xml_code.xpath('//notebook/user[last()]/name/text()')
#選取notebook下倒數(shù)第二個(gè)user的name屬性
print xml_code.xpath('//notebook/user[last()-1]/name/text()')
#選取notebook下前兩名user的address屬性
print xml_code.xpath('//notebook/user[position()<3]/address/text()')
#選取所有分類為web的name
print xml_code.xpath('//notebook/user[@category="cb"]/name/text()')
#選取所有年齡小于30的人
print xml_code.xpath('//notebook/user[age<30]/name/text()')
#選取所有class屬性中包含dba的class屬性
print xml_code.xpath('//notebook/user[contains(@class,"dba")]/@class')
print xml_code.xpath('//notebook/user[contains(@class,"dba")]/name/text()')
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。