惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 叶小钗
大猫的无限游戏
大猫的无限游戏
H
Help Net Security
G
Google Developers Blog
D
Docker
阮一峰的网络日志
阮一峰的网络日志
A
About on SuperTechFans
aimingoo的专栏
aimingoo的专栏
博客园 - 聂微东
Hugging Face - Blog
Hugging Face - Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Apple Machine Learning Research
Apple Machine Learning Research
云风的 BLOG
云风的 BLOG
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
腾讯CDC
T
The Blog of Author Tim Ferriss
Microsoft Security Blog
Microsoft Security Blog
WordPress大学
WordPress大学
I
InfoQ
Engineering at Meta
Engineering at Meta
Stack Overflow Blog
Stack Overflow Blog
Google DeepMind News
Google DeepMind News

Go Further

【教程】manim动画制作工具 【直观算法】二叉搜索树算法总结 【直观算法】二叉树遍历算法总结 【直观算法】Egg Puzzle 鸡蛋难题 博客文章总目录 TensorSpace 一个3D神经网络可视化框架 【直观详解】通俗易懂了解什么是黎曼猜想 支持币与去中心化商业模式 那些值得一看的TED演讲附全文文稿笔记 【区块链】共识算法与如何解决拜占庭将军问题 【直观详解】让你永远忘不了的傅里叶变换解析 【直观详解】泰勒级数 微信跳一跳解题报告 Dota2-A帐效果 深入浅出看懂AlphaGo Zero/AlphaGo元 【直观详解】线性代数中的转置正交正规正定 程序员技能图谱 【直观详解】线性代数的本质 【直观详解】什么是PCA、SVD 【直观详解】什么是正则化 Pandas-Wiki 【区块链】比特币与金融、ICO和监管 【区块链】现代区块链与新技术 【区块链】一文看懂区块链:一步一步发明比特币 【直观详解】拉格朗日乘法和KKT条件 【直观详解】支持向量机SVM Dota2伤害类型详解 【直观详解】机器学习分类器性能指标详解 【直观详解】信息熵、交叉熵和相对熵 Dota2机制总结
Xpath-Wiki
2017-08-28 · via Go Further

Xpath-Wiki

【阅读时间】查阅类文档
【内容简介】Xpath相关使用法法和例子文档,以供查阅(➜ 后是对应语句的输出output)

XPath 相关例子Note

例子1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from lxml import etree
sample1 = """<html>
<head>
<title>My page</title>
</head>
<body>
<h2>Welcome to my <a href="#" src="x">page</a></h2>
<p>This is the first paragraph.</p>
<!-- this is the end -->
</body>
</html>
"""
def getxpath(html):
return etree.HTML(html)
s1 = getxpath(sample1)

//绝对路径 text() 获取内容中的文字信息

1
s1.xpath('//title/text()') ➜ ['My page']

/ 相对路径

1
s1.xpath('/html/head/title/text()') ➜ ['My page']

获取属性src的值

1
s1.xpath('//h2/a/@src') ➜ ['x']

获取所有属性href的值

1
s1.xpath('//@href') ➜ ['#']

获取网页中的所有文本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
s1.xpath('//text()')

['\n ',
'\n ',
'My page',
'\n ',
'\n ',
'\n ',
'Welcome to my ',
'page',
'\n ',
'This is the first paragraph.',
'\n ',
'\n ',
'\n']

获取网页中的所有注释

1
s1.xpath('//comment()') ➜ [<!-- this is the end -->]

例子2

1
2
3
4
5
6
7
8
9
10
11
12
13
sample2 = """
<html>
<body>
<ul>
<li>Quote 1</li>
<li>Quote 2 with <a href="...">link</a></li>
<li>Quote 3 with <a href="...">another link</a></li>
<li><h2>Quote 4 title</h2>Something here.</li>
</ul>
</body>
</html>
"""
s2 = getxpath(sample2)

获取所有li中的文本

1
s2.xpath('//li/text()') ➜ ['Quote 1', 'Quote 2 with ', 'Quote 3 with ', 'Something here.']

获取第一个 第二个li中的文本,两种写法均可

1
s2.xpath('//li[position() = 1]/text()') ➜ ['Quote 1']
1
s2.xpath('//li[1]/text()') ➜ ['Quote 1']
1
s2.xpath('//li[position() = 2]/text()') ➜ ['Quote 2 with ']
1
s2.xpath('//li[2]/text()') ➜ ['Quote 2 with ']

奇数 偶数 最后一个

1
s2.xpath('//li[position() mod2 = 1]/text()') ➜ ['Quote 1', 'Quote 3 with ']
1
s2.xpath('//li[position() mod2 = 0]/text()') ➜ ['Quote 2 with ', 'Something here.']
1
s2.xpath('//li[last()]/text()') ➜ ['Something here.']

li下面a中的文本

1
s2.xpath('//li[a]/text()') ➜ ['Quote 2 with ', 'Quote 3 with ']

lia或者h2的文本

1
s2.xpath('//li[a or h2]/text()') ➜ ['Quote 2 with ', 'Quote 3 with ', 'Something here.']

使用 | 同时获取 a 和 h2 中的内容

1
s2.xpath('//a/text()|//h2/text()') ➜ ['link', 'another link', 'Quote 4 title']

例子3

1
2
3
4
5
6
7
8
9
10
11
12
13
sample3 = """<html>
<body>
<ul>
<li id="begin"><a href="https://scrapy.org">Scrapy</a>begin</li>
<li><a href="https://scrapinghub.com">Scrapinghub</a></li>
<li><a href="https://blog.scrapinghub.com">Scrapinghub Blog</a></li>
<li id="end"><a href="http://quotes.toscrape.com">Quotes To Scrape</a>end</li>
<li data-xxxx="end" abc="abc"><a href="http://quotes.toscrape.com">Quotes To Scrape</a>end</li>
</ul>
</body>
</html>
"""
s3 = getxpath(sample3)

获取 a 标签下 href 以https开始的

1
s3.xpath('//a[starts-with(@href, "https")]/text()') ➜ ['Scrapy', 'Scrapinghub', 'Scrapinghub Blog']

获取 href=https://scrapy.org

1
s3.xpath('//li/a[@href="https://scrapy.org"]/text()') ➜ ['Scrapy']

获取 id = begin

1
s3.xpath('//li[@id="begin"]/text()') ➜ ['begin']

获取text = Scrapinghub

1
s3.xpath('//li/a[text()="Scrapinghub"]/text()') ➜ ['Scrapinghub']

获取某个标签下 某个参数 = xx

1
s3.xpath('//li[@data-xxxx="end"]/text()') ➜ ['end']
1
s3.xpath('//li[@abc="abc"]/text()') ➜ ['end']

例子4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
sample4 = u"""
<html>
<head>
<title>My page</title>
</head>
<body>
<h2>Welcome to my <a href="#" src="x">page</a></h2>
<p>This is the first paragraph.</p>
<p class="test">
编程语言<a href="#">python</a>
<img src="#" alt="test"/>javascript
<a href="#"><strong>C#</strong>JAVA</a>
</p>
<p class="content-a">a</p>
<p class="content-b">b</p>
<p class="content-c">c</p>
<p class="content-d">d</p>
<p class="econtent-e">e</p>
<!-- this is the end -->
</body>
</html>
"""
s4 = etree.HTML(sample4)

获取 class = test 标签中的所有文字

1
2
s4.xpath('//p[@class="test"]/text()')
➜ ['\n 编程语言', '\n ', 'javascript\n ', '\n ']

使用String来获得文字段; strip() 移除字符串收尾字符,默认为空格

1
2
3
4
5
print (s4.xpath('string(//p[@class="test"])').strip())

编程语言python
javascript
C

获取所有class属性中以content开始

1
s4.xpath('//p[starts-with(@class,"content")]/text()') ➜ ['a', 'b', 'c', 'd']

获取所有class属性中包含content的

1
s4.xpath(('//*[contains(@class,"content")]/text()')) ➜ ['a', 'b', 'c', 'd', 'e']