Python爬虫:xpath常用方法示例

简介: Python爬虫:xpath常用方法示例
# -*-coding:utf-8-*-
html = """
<html>
 <head>
  <base href='http://example.com/' />
  <title>Example website</title>
 </head>
 <body>
  <div id='images'>
   <a href='image1.html'>Name: My image 1 <br /><img src='image1_thumb.jpg' /></a>
   <a href='image2.html'>Name: My image 2 <br /><img src='image2_thumb.jpg' /></a>
   <a href='image3.html'>Name: My image 3 <br /><img src='image3_thumb.jpg' /></a>
   <a href='image4.html'>Name: My image 4 <br /><img src='image4_thumb.jpg' /></a>
   <a href='image5.html'>Name: My image 5 <br /><img src='image5_thumb.jpg' /></a>
  </div>
 </body>
</html>
"""
from scrapy.selector import Selector
sel = Selector(text=html)
print("================title===============")
title_by_xpath = sel.xpath("//title//text()").extract_first()
print(title_by_xpath)
title_by_css = sel.css("title::text").extract_first()
print(title_by_css)
print("================href===============")
hrefs = sel.xpath("//a/@href").extract()
print(hrefs)
hrefs_by_css = sel.css("a::attr(href)").extract()
print(hrefs_by_css)
print("================img===============")
imgs = sel.xpath("//a[contains(@href, 'image')]/@href").extract()
print(imgs)
imgs_by_css = sel.css("a[href*=image]::attr(href)").extract()
print(imgs_by_css)
print("================src===============")
src = sel.xpath("//a[contains(@href, 'image')]/img/@src").extract()
print(src)
src_by_css = sel.css("a[href*=image] img::attr(src)").extract()
print(src_by_css)
print("================ re ===============")
text_by_re = sel.css("a[href*=image]::text").re(r"Name:\s*(.*)")
print(text_by_re)
print("================ xpath ===============")
div = sel.xpath("//div")  # 相对路径
print(div)
a = div.xpath(".//a").extract() # 从当前提取所有元素
print(a)
print("================ text ===============")
text='<a href="#">Click here to go to the <strong>Next Page</strong></a>'
sel1 = Selector(text=text)
# a下面的文字
a = sel1.xpath("//a/text()").extract()
print(a)
# a 下面所有的文字,包括strong
a = sel1.xpath("//a//text()").extract()
print(a)
# 解析出所有文字内容
a = sel1.xpath("string(//a)").extract()
print(a)
a = sel1.xpath("string(.)").extract()
print(a)
# 简化写法,推荐
xp = lambda x: sel.xpath(x).extract()
all_a = xp("//a/text()")
print(all_a)
相关文章
|
8月前
|
测试技术 开发者 Python
Python单元测试入门:3个核心断言方法,帮你快速定位代码bug
本文介绍Python单元测试基础,详解`unittest`框架中的三大核心断言方法:`assertEqual`验证值相等,`assertTrue`和`assertFalse`判断条件真假。通过实例演示其用法,帮助开发者自动化检测代码逻辑,提升测试效率与可靠性。
546 1
|
9月前
|
机器学习/深度学习 数据采集 数据挖掘
基于 GARCH -LSTM 模型的混合方法进行时间序列预测研究(Python代码实现)
基于 GARCH -LSTM 模型的混合方法进行时间序列预测研究(Python代码实现)
307 2
|
9月前
|
调度 Python
微电网两阶段鲁棒优化经济调度方法(Python代码实现)
微电网两阶段鲁棒优化经济调度方法(Python代码实现)
241 0
|
9月前
|
传感器 大数据 API
Python数字限制在指定范围内:方法与实践
在Python编程中,限制数字范围是常见需求,如游戏属性控制、金融计算和数据过滤等场景。本文介绍了五种主流方法:基础条件判断、数学运算、装饰器模式、类封装及NumPy数组处理,分别适用于不同复杂度和性能要求的场景。每种方法均有示例代码和适用情况说明,帮助开发者根据实际需求选择最优方案。
413 0
|
8月前
|
人工智能 数据安全/隐私保护 异构计算
桌面版exe安装和Python命令行安装2种方法详细讲解图片去水印AI源码私有化部署Lama-Cleaner安装使用方法-优雅草卓伊凡
桌面版exe安装和Python命令行安装2种方法详细讲解图片去水印AI源码私有化部署Lama-Cleaner安装使用方法-优雅草卓伊凡
1194 8
桌面版exe安装和Python命令行安装2种方法详细讲解图片去水印AI源码私有化部署Lama-Cleaner安装使用方法-优雅草卓伊凡
|
9月前
|
机器学习/深度学习 数据采集 算法
【CNN-BiLSTM-attention】基于高斯混合模型聚类的风电场短期功率预测方法(Python&matlab代码实现)
【CNN-BiLSTM-attention】基于高斯混合模型聚类的风电场短期功率预测方法(Python&matlab代码实现)
440 4
|
8月前
|
算法 调度 决策智能
【两阶段鲁棒优化】利用列-约束生成方法求解两阶段鲁棒优化问题(Python代码实现)
【两阶段鲁棒优化】利用列-约束生成方法求解两阶段鲁棒优化问题(Python代码实现)
240 0
|
9月前
|
机器学习/深度学习 数据采集 TensorFlow
基于CNN-GRU-Attention混合神经网络的负荷预测方法(Python代码实现)
基于CNN-GRU-Attention混合神经网络的负荷预测方法(Python代码实现)
470 0
|
9月前
|
数据采集 索引 Python
Python Slice函数使用教程 - 详解与示例 | Python切片操作指南
Python中的`slice()`函数用于创建切片对象,以便对序列(如列表、字符串、元组)进行高效切片操作。它支持指定起始索引、结束索引和步长,提升代码可读性和灵活性。
|
数据采集 测试技术 C++
无headers爬虫 vs 带headers爬虫:Python性能对比
无headers爬虫 vs 带headers爬虫:Python性能对比

推荐镜像

更多