Python使用xpath对解析内容进行数据提取

XPath 使用路径表达式来选取HTML/ XML 文档中的节点或节点集,节点是通过沿着路径 (path) 或者步 (steps) 来选取的,本文将给大家介绍Python使用xpath对解析内容进行数据提取的方法,需要的朋友可以参考下

一、前言
在前面的文章当中,已经教大家如何去获取我们需要的数据原文内容,今天就介绍一个用于提取所需数据的方法之一xpath。在后续会讲解bs4(beautifulsoup),re正则表达式。

二、正文
XPath 使用路径表达式来选取HTML/ XML 文档中的节点或节点集。节点是通过沿着路径 (path) 或者步 (steps) 来选取的。

使用到python中的一个lxml库:下载 pip install lxml

选取节点

路径表达式

谓语
谓语用来查找某个特定的节点或者包含某个指定的值的节点。

谓语被嵌在方括号中。

选取未知节点

--- 在下面的表格中,我们列出了一些路径表达式,以及这些表达式的结果:

选取若干节点
通过在路径表达式中使用"|"运算符,您可以选取若干个路径。

三、示例
下面给出一个示例代码

# -*- coding:utf-8 -*-
import requests
from lxml import etree
​
​
class DouGuo(object):
    def __init__(self):
        self.url = "https://www.douguo.com/caipu/%E5%AE%B6%E5%B8%B8%E8%8F%9C/0/20"
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
        }
​
    def get_data_index(self):
        response = requests.get(self.url, headers=self.headers)
        response.encoding="utf-8"
        if response.status_code == 200:
            return response.text
        else:
            return None
​
    def parse_data_index(self, response):
        html = etree.HTML(response)
        data_list = html.xpath('//ul[@class="cook-list"]//li[@class="clearfix"]')
        for data in data_list:
            # 提取文本值
            title = data.xpath("./div/a/text()")[0]
            major = data.xpath("./div/p/text()")[0]
            # 提取属性值
            head = data.xpath("./div/div[2]/a/img/@alt")[0]
            score = data.xpath("./div/div[1]//span/text()")[0]
            print(f"title: {title}\nmajor: {major}\nhead:{head}\nscore:{score}\n\n")
​
    def run(self):
        response = self.get_data_index()
        # print(response)
        self.parse_data_index(response)
​
if __name__ == '__main__':
    spider = DouGuo()
    spider.run()
复制
 # -*- coding:utf-8 -*-
 import requests
 from lxml import etree
 ​
 ​
 class DouGuo(object):
     def __init__(self):
         self.url = "https://www.douguo.com/caipu/%E5%AE%B6%E5%B8%B8%E8%8F%9C/0/20"
         self.headers = {
             "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
         }
 ​
     def get_data_index(self):
         response = requests.get(self.url, headers=self.headers)
         response.encoding="utf-8"
         if response.status_code == 200:
             return response.text
         else:
             return None
 ​
     def parse_data_index(self, response):
         html = etree.HTML(response)
         data_list = html.xpath('//ul[@class="cook-list"]//li[@class="clearfix"]')
         for data in data_list:
             # 提取文本值
             title = data.xpath("./div/a/text()")[0]
             major = data.xpath("./div/p/text()")[0]
             # 提取属性值
             head = data.xpath("./div/div[2]/a/img/@alt")[0]
             score = data.xpath("./div/div[1]//span/text()")[0]
             print(f"title: {title}\nmajor: {major}\nhead:{head}\nscore:{score}\n\n")
 ​
     def run(self):
         response = self.get_data_index()
         # print(response)
         self.parse_data_index(response)
 ​
 if __name__ == '__main__':
     spider = DouGuo()
     spider.run()

四、结语
大家可以尝试去抓取这个url 长沙二手房房源_长沙二手房出售|买卖|交易信息(长沙链家)

获取第一页数据即可,同时也可以思考一下,如何进行多页的获取,实现翻页功能。

到此这篇关于Python使用xpath对解析内容进行数据提取的文章就介绍到这了,更多相关Python xpath数据提取内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持vb.net教程C#教程python教程SQL教程access 2010教程Visual Basic 2010 2012 2013 从入门到精通|xin3721自学网

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值