0
点赞
收藏
分享

微信扫一扫

【爬虫基础_4】爬虫xpath

承蒙不弃 2022-04-14 阅读 54
python

XP ath 语句

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


目录


XML 实例文档

实例
<?xmlversion="1.0"encoding="UTF-8"?>
<bookstore>
	<book>
		<titlelang="eng">HarryPotter</title>
		<price>29.99</price>
</book>
<book>
		<titlelang="eng">LearningXML</title>
		<price>39.95</price>
</book>
</bookstore>

提示:以下是本篇文章正文内容,下面案例可供参考

一、Xpath的作用:

XPath是一门在XML文档中查找信息的语言。
XML文档包括:
HTML/XHTMLXML/XMLNamespaces

二、XPath的节点:

请看这个XML 文档:

<?xmlversion="1.0"encoding="UTF-8"?>

<bookstore>
	<book>
		<title lang="en">Harry Potter</title>
		<author>JK.Rowling</author>
		<year>2005</year>
		<price>29.99</price>
	</book>
</bookstore>

上面的XML文档中的节点例子:

<bookstore>(文档节点)

<author>JK.Rowling</author>(元素节点)

lang="en"(属性节点)

节点关系

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
import  ssl
ssl._create_default_https_context = ssl._create_unverified_context

1.父(parent)


每个元素以及属性都有一个父。在下面的例子中,book元素是title、author、year以及price元素的父:

<book>
	<title>HarryPotter</title>
	<author>JK.Rowling</author>
	<year>2005</year>
	<price>29.99</price>
</book>

2.子(Children)

元素节点可有零个、一个或多个子。在下面的例子中,title、author、year以及price元素都是book元素的子:

<book>
	<title>HarryPotter</title>
	<author>JK.Rowling</author>
	<year>2005</year>
	<price>29.99</price>
</book>

3.同胞(Sibling)

拥有相同的父的节点在下面的例子中,title、author、year以及price元素都是同胞:

<book>
	<title>HarryPotter</title>
	<author>JK.Rowling</author>
	<year>2005</year>
	<price>29.99</price>
</book>

4.先辈(Ancestor)

某节点的父、父的父,等等。在下面的例子中,title元素的先辈是book元素和bookstore元素:

<bookstore>
	<book>
		<title>HarryPotter</title>
		<author>JK.Rowling</author>
		<year>2005</year>
		<price>29.99</price>
	</book>
</bookstore>
举报

相关推荐

0 条评论