Xpath实用方法

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/gklcsdn/article/details/101947564

Xpath 常用方法总结

1. Xpath常用规则及方法
表达式 描述
/ 从当前节点选取直接子节点
// 从当前节点选取所有子节点
. 代表当前节点
当前节点的父节点
@ 选取当前节点的属性
text() 获取文本信息
string() 获取文本信息
2. 网页源代码
html = '''
    <html>
        <body>
            <bookstore>
                <book category="COOKING">
                    <title lang="en">Everyday Italian</title>
                    <author>Giada De Laurentiis</author>
                    <year>2005</year>
                    <price class="li li-price">30.00</price>
                </book>

                <book category="CHILDREN">
                    <title lang="en">Harry Potter</title>
                    <author>J K. Rowling</author>
                    <year>2005</year>
                    <price class="li li-price">29.99</price>
                </book>

                <book category="WEB">
                    <title lang="en">XQuery Kick Start</title>
                    <author>James McGovern</author>
                    <author>Per Bothner</author>
                    <author>Kurt Cagle</author>
                    <author>James Linn</author>
                    <author>Vaidyanathan Nagarajan</author>
                    <year>2003</year>
                    <price class="li li-price">49.99</price>
                </book>

                <book category="WEB">
                    <title lang="en">Learning XML</title>
                    <author>Erik T. Ray</author>
                    <year>2003</year>
                    <price class="li li-price">39.95</price>
                </book>
            </bookstore>
        </body>
    </html>
'''
3. 获取所有书的价格
# 模块导入
from lxml import etree

# 获取一个Xpath解析对象
page = etree.HTML(html)
# 获取所有书的价格
price = page.xpath('//price[@class="li li-price"]/text()')
4. 获取特定某一本书的价格
from lxml import etree

page = etree.HTML(html)

# 獲取第一本書的價格(索引從1開始)
book_first = page.xpath("//book[1]/price/text()")

# 獲取最後一本書的價格
book_last = page.xpath("//book[last()]/price/text()")

# 獲取前三本書的價格
book_three = page.xpath("//book[position()<4]/price/text()")
5. 选取多个路径
In [1]: page.xpath('//book[1]/title/text() | //book[1]/author/text()')
Out[1]: ['Everyday Italian', 'Giada De Laurentiis']
6. 提取每本书的详情
page = etree.HTML(html)
node_list = page.xpath("//book")
for node in node_list:
    title = node.xpath("./title/text()")[0]
    author = node.xpath("./author/text()")[0]
    year = node.xpath("./year/text()")[0]
    price = node.xpath("./price/text()")[0]

    items = {
        '標題': title,
        '作者': author,
        '年份': year,
        '價格': price
    }
    print(items)
    
>>>>
{'標題': 'Everyday Italian', '作者': 'Giada De Laurentiis', '年份': '2005', '價格': '30.00'}
{'標題': 'Harry Potter', '作者': 'J K. Rowling', '年份': '2005', '價格': '29.99'}
{'標題': 'XQuery Kick Start', '作者': 'James McGovern', '年份': '2003', '價格': '49.99'}
{'標題': 'Learning XML', '作者': 'Erik T. Ray', '年份': '2003', '價格': '39.95'}

总结

Xpath的选择功能十分强大, 熟练掌握Xpath的使用能大大的提高数据提取效率;
以上几种方法几乎能应对所有结构化数据的提取了

猜你喜欢

转载自blog.csdn.net/gklcsdn/article/details/101947564