本文为自学记录,部分内容转载于

w3school

python3网络爬虫实战

知乎专栏:写点python

如有侵权,请联系删除。

语法

1、选取节点

XPath 使用路径表达式在 XML 文档中选取节点。节点是通过沿着路径或者 step 来选取的。

实例解释

2、谓语

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

谓语被嵌在方括号中。

text ='''
<bookstore>
<book>
<title lang="eng">Harry Potter</title>
<price>29.99</price>
</book>
<book>
<title lang="eng">Learning XML</title>
<price>39.95</price>
</book>
</bookstore>
''' from lxml import etree # 初始化,构造xpath解析对象
html = etree.HTML(text)
# 修正HTML代码,结果为bytes类型,还需要转化为str类型
# html = etree.tostring(html,encoding='utf-8')
result = html.xpath('//@lang')
print(result) 结果:
['eng', 'eng']

3、选取未知节点

4、选取若干路径

实例

from lxml import etree
myPage = '''<html>
<title>TITLE</title>
<body>
<h1>我的博客</h1>
<div>我的文章</div>
<div id="photos">
<img src="pic1.jpeg"/><span id="pic1">PIC1 is beautiful!</span>
<img src="pic2.jpeg"/><span id="pic2">PIC2 is beautiful!</span>
<p><a href="http://www.example.com/more_pic.html">更多美图</a></p>
<a href="http://www.baidu.com">去往百度</a>
<a href="http://www.163.com">去往网易</a>
<a href="http://www.sohu.com">去往搜狐</a>
</div>
<p class="myclassname">Hello,\nworld!<br/>-- by Adam</p>
<div class="foot">放在尾部的其他一些说明</div>
</body>
</html>''' html = etree.HTML(myPage) # 定位
div1 = html.xpath('//div')
div2 = html.xpath('//div[@id]')
div3 = html.xpath('//div[@class="foot"]')
div4 = html.xpath('//div[@*]')
div5 = html.xpath('//div[1]')
div6 = html.xpath('//div[last()-1]')
div7 = html.xpath('//div[position()<3]')
div8 = html.xpath('//div|//h1')
div9 = html.xpath('//div[not(@*)]') # 取文本text()区别html.xpath('string()')
text1 = html.xpath('//div/text()')
text2 = html.xpath('//div[@id]/text()')
text3 = html.xpath('//div[@class="foot"]/text()')
text4 = html.xpath('//div[@*]/text()')
text5 = html.xpath('//div[1]/text()')
text6 = html.xpath('//div[last()-1]/text()')
text7 = html.xpath('//div[position()<3]/text()')
text8 = html.xpath('//div/text()|//h1/text()')
text9 = html.xpath('//div[not(@*)]/text()') # 取属性
value1 = html.xpath('//a/@href')
value2 = html.xpath('//img/@src')
value3 = html.xpath('div[2]/span/@id') # print(value1)
# print(value2)
# print(value3)
# 定位(进阶)
a_href = html.xpath('//div[position()<3]/a/@href')
print(a_href) a_href = html.xpath('//div[position()<3]//a/@href')
print(a_href)

实例2

在上一篇爬取猫眼TOP100里面,我们是用正则表达式去进行解析的,这里用xpath进行解析测试

from lxml import etree
html = '''
<dd>
<i class="board-index board-index-1">1</i>
<a href="/films/1203" title="霸王别姬" class="image-link" data-act="boarditem-click" data-val="{movieId:1203}">
<img src="//s0.meituan.net/bs/?f=myfe/mywww:/image/loading_2.e3d934bf.png" alt="" class="poster-default" />
<img data-src="https://p1.meituan.net/movie/20803f59291c47e1e116c11963ce019e68711.jpg@160w_220h_1e_1c" alt="霸王别姬" class="board-img" />
</a>
<div class="board-item-main">
<div class="board-item-content">
<div class="movie-item-info">
<p class="name"><a href="/films/1203" title="霸王别姬" data-act="boarditem-click" data-val="{movieId:1203}">霸王别姬</a></p>
<p class="star">
主演:张国荣,张丰毅,巩俐
</p>
<p class="releasetime">上映时间:1993-01-01</p> </div>
<div class="movie-item-number score-num">
<p class="score"><i class="integer">9.</i><i class="fraction">5</i></p>
</div> </div>
</div> </dd>
''' # 初始化
html = etree.HTML(html)
# html = etree.tostring(html, encoding='utf-8')
# print(html.decode('utf-8'))
# print(type(html))
# print(html)
# 排名
index = html.xpath('//dd/i[@class="board-index board-index-1"]/text()')
print(index)
# 图片
image = html.xpath('//dd/a/img/@data-src')
print(image)
# 标题
title = html.xpath('//dd/div/div/div/p/a/text()')
print(title)
# 演员
star = html.xpath('//dd/div/div/div/p[@class="star"]/text()')
print(star)
# 上映时间
time = html.xpath('//dd/div/div/div/p[@class="releasetime"]/text()')
print(time)
# 评分
sorce = html.xpath('//dd/div/div/div/p/i[@class="integer"]/text()')
print(sorce)
sorce2 = html.xpath('//dd/div/div/div/p/i[@class="fraction"]/text()')
print(sorce2)

结果:

【爬虫】-xpath语法熟悉及实战的更多相关文章

  1. Python爬虫之xpath语法及案例使用

    Python爬虫之xpath语法及案例使用 ---- 钢铁侠的知识库 2022.08.15 我们在写Python爬虫时,经常需要对网页提取信息,如果用传统正则表达去写会增加很多工作量,此时需要一种对数 ...

  2. 爬虫解析之css,xpath语法

    一.xpath语法 xpath实例文档 <?xml version="1.0" encoding="ISO-8859-1"?> <bookst ...

  3. python爬虫:XPath语法和使用示例

    python爬虫:XPath语法和使用示例 XPath(XML Path Language)是一门在XML文档中查找信息的语言,可以用来在XML文档中对元素和属性进行遍历. 选取节点 XPath使用路 ...

  4. Python爬虫利器三之Xpath语法与lxml库的用法

    前面我们介绍了 BeautifulSoup 的用法,这个已经是非常强大的库了,不过还有一些比较流行的解析库,例如 lxml,使用的是 Xpath 语法,同样是效率比较高的解析方法.如果大家对 Beau ...

  5. 常见的爬虫分析库(2)-xpath语法

    xpath简介 1.xpath使用路径表达式在xml和html中进行导航 2.xpath包含标准函数库 3.xpath是一个w3c的标准 xpath节点关系 1.父节点 2.子节点 3.同胞节点 4. ...

  6. Xpath语法-爬虫(一)

    前言 这一章节主要讲解Xpath的基础语法,学习如何通过Xpath获取网页中我们想要的内容;为我们的后面学习Java网络爬虫基础准备工作. 备注:此章节为基础核心章节,未来会在网络爬虫的数据解析环节经 ...

  7. 芝麻HTTP:Python爬虫利器之Xpath语法与lxml库的用法

    安装 ​pip install lxml 利用 pip 安装即可 XPath语法 XPath 是一门在 XML 文档中查找信息的语言.XPath 可用来在 XML 文档中对元素和属性进行遍历.XPat ...

  8. 12.Python爬虫利器三之Xpath语法与lxml库的用法

    LXML解析库使用的是Xpath语法: XPath 是一门语言 XPath可以在XML文档中查找信息 XPath支持HTML XPath通过元素和属性进行导航 XPath可以用来提取信息 XPath比 ...

  9. [Python 练习爬虫] XPATH基础语法

    XPATH语法: // 定位根标签 / 往下层寻找 /text() 提取文本内容 /@xxx 提取属性内容 Sample: import requests from lxml import etree ...

随机推荐

  1. SpringMVC源码解读 - HandlerMapping - AbstractDetectingUrlHandlerMapping系列初始化

    AbstractDetectingUrlHandlerMapping是通过扫描方式注册Handler,收到请求时由AbstractUrlHandlerMapping的getHandlerInterna ...

  2. 在Mac OS下配置PHP开发环境

    实在厌倦了windows无缘无故的宕机.病毒了吗,哈哈哈,这个跟我都没什么关系.准备使用下现如今牛X到不行的云平台没有办法只好研究下PHP. 现在的云平台支持的语言只有PHP.Java和Python. ...

  3. 学习python5面向

    类有一个名为 __init__() 的特殊方法(构造方法),该方法在类实例化时会自动调用 面向过程:根据业务逻辑从上到下写代码 面向对象:将数据与函数绑定到一起,进行封装,这样能够更快速的开发程序,减 ...

  4. win10家庭版系统安装SQL2005

    安装sql2005花了两天的时间,现在总结下,刚开始不知道win10家庭版系统没有本地用户和组的(详细说明 https://www.kafan.cn/A/5vz17o5jne.html) 一定要升级到 ...

  5. 电脑连接真机,但是androidstudio不显示手机,ADB Interface黄色感叹号

    1.打开设备管理器 右键计算机 -->管理-->选择设备管理器 2. 步骤如下: 1. 在设备管理器中,找到ADB Interface,右击,选择“更新驱动程序软件” 2. 在弹出的窗口中 ...

  6. [LeetCode 题解]: Permutations

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  7. 关于C#/sqlserver生成32位数据库字段总结

    一.C#中用Guid.NewGuid().ToString() Sql中用NEWID()   以上方法生成的是36位的GUID,如果需要转换成32位,则需要替换掉其中的'-'字符. Sql中的方法:r ...

  8. 【《Effective C#》提炼总结】提高Unity中C#代码质量的22条准则

    引言 原则1尽可能地使用属性而不是可直接访问的数据成员 原则2偏向于使用运行时常量而不是编译时常量 原则3 推荐使用is 或as操作符而不是强制类型转换 原则4 推荐使用条件属性而不是if条件编译 原 ...

  9. 1.css介绍

    CSS介绍 我们为什么需要CSS? 使用css的目的就是让网页具有美观一致的页面,另外一个最重要的原因是内容与格式分离 在没有CSS之前,我们想要修改HTML元素的样式需要为每个HTML元素单独定义样 ...

  10. Layout2:StackPanel(补交作业)

    <StackPanel Orientation="Horizontal" VerticalAlignment="Bottom" > <Rect ...