当前位置: 首页 > news >正文

天河建设网站开发网站seo顾问

天河建设网站开发,网站seo顾问,西宁网站建设排名,长沙网络营销公司有哪些对于网页的节点来说,它可以定义 id、class 或其他属性。而且节点之间还有层次关系,在网页中可以通过 XPath 或 CSS 选择器来定位一个或多个节点。在页面解析时,利用 XPath 或 CSS 选择器来提取某个节点,然后再调用相应方法获取它的…

对于网页的节点来说,它可以定义 id、class 或其他属性。而且节点之间还有层次关系,在网页中可以通过 XPath 或 CSS 选择器来定位一个或多个节点。在页面解析时,利用 XPath 或 CSS 选择器来提取某个节点,然后再调用相应方法获取它的正文内容或者属性,就可以提取我们想要的任意信息。

这种解析库已经非常多,其中比较强大的库有 lxml、Beautiful Soup、pyquery 等,通过使用解析库,可以免去编写正则表达式的麻烦,解析效率也能有所提高。

XPath的使用

XPath,全称 XML Path Language,即 XML 路径语言,它是一门在 XML 文档中查找信息的语言。它最初是用来搜寻 XML 文档的,但是它同样适用于 HTML 文档的搜索。所以在做爬虫时,我们完全可以使用 XPath 来做相应的信息抽取。XPath 的选择功能十分强大,它提供了非常简洁明了的路径选择表达式。另外,它还提供了超过 100 个内建函数,用于字符串、数值、时间的匹配以及节点、序列的处理等。几乎所有我们想要定位的节点,都可以用 XPath 来选择。

XPath 常用规则

表 达 式描  述
nodename选取所有name为nodename的结点
/从根节点开始选取
//从当前节点开始选取html中所有匹配选取要求的结点
.选取当前节点
选取当前节点的父节点
@选取属性
Path ExpressionResult
bookstore选择所有名称为 “bookstore” 的节点
/bookstore选择根元素 bookstore
注意:如果路径以斜杠(/)开头,则始终表示对元素的绝对路径!
bookstore/book选择 bookstore 的所有子元素中的 book 元素
//book选择文档中的所有 book 元素,无论它们在哪里
bookstore//book选择 bookstore 元素下的所有后代 book 元素,无论它们在 bookstore 元素下的哪个位置
//@lang选择所有名为 lang 的属性

实例

假设html代码如下(下面为python代码),开始前请安装lxml包:

text = '''
<div><ul><li class="item-0"><a href="link1.html">first item</a></li><li class="item-1"><a href="link2.html">second item</a></li><li class="item-inactive"><a href="link3.html">third item</a></li><li class="item-1"><a href="link4.html">fourth item</a></li><li class="item-0"><a href="link5.html">fifth item</a></ul></div>
'''

对这段html进行解析:

from lxml import etree
html = etree.HTML(text)
result = etree.tostring(html)
print(result.decode('utf-8'))

这里首先导入 lxml 库的 etree 模块,然后声明了一段 HTML 文本,调用 HTML 类进行初始化,这样就成功构造了一个 XPath 解析对象。这里需要注意的是,HTML 文本中的最后一个 li 节点是没有闭合的,但是 etree 模块可以自动修正 HTML 文本。

这里我们调用 tostring 方法即可输出修正后的 HTML 代码,但是结果是 bytes 类型。这里利用 decode 方法将其转成 str 类型。

<html><body><div><ul><li class="item-0"><a href="link1.html">first item</a></li><li class="item-1"><a href="link2.html">second item</a></li><li class="item-inactive"><a href="link3.html">third item</a></li><li class="item-1"><a href="link4.html">fourth item</a></li><li class="item-0"><a href="link5.html">fifth item</a></li></ul></div>
</body></html>

经过处理之后,li 节点标签被补全,并且还自动添加了 body、html 节点。

也可以直接读取文本文件进行解析:

html = etree.parse('./test.html', etree.HTMLParser())

输出结果会多一个 DOCTYPE 的声明。

选取所有节点

一般会用 // 开头的 XPath 规则来选取所有符合要求的节点。以前面的 HTML 文本为例,如果要选取所有节点,可以这样实现:

result = html.xpath('//*')

运行结果:

[<Element html at 0x10510d9c8>, <Element body at 0x10510da08>, <Element div at 0x10510da48>, <Element ul at 0x10510da88>, <Element li at 0x10510dac8>, <Element a at 0x10510db48>, <Element li at 0x10510db88>, <Element a at 0x10510dbc8>, <Element li at 0x10510dc08>, <Element a at 0x10510db08>, <Element li at 0x10510dc48>, <Element a at 0x10510dc88>, <Element li at 0x10510dcc8>, <Element a at 0x10510dd08>]

这里使用 * 代表匹配所有节点,也就是整个 HTML 文本中的所有节点都会被获取。可以看到,返回形式是一个列表,每个元素是 Element 类型,其后跟了节点的名称,如 html、body、div、ul、li、a 等,所有节点都包含在列表中了。

如果想获取所有 li 节点:

result = html.xpath('//li')

提取结果是一个列表形式,其中每个元素都是一个 Element 对象。如果要取出其中一个对象,可以直接用中括号加索引,如 [0]。

选取子节点

通过 / 或 // 即可查找元素的子节点或子孙节点。假如现在想选择 li 节点的所有直接 a 子节点:

result = html.xpath('//li/a')

通过追加 /a 即选择了所有 li 节点的所有直接 a 子节点。因为 //li 用于选中所有 li 节点,/a 用于选中 li 节点的所有直接子节点 a,二者组合在一起即获取所有 li 节点的所有直接 a 子节点。

此处的 / 用于选取直接子节点,如果要获取所有子孙节点,就可以使用 //:

result = html.xpath('//ul//a')

本例中运行结果相同。但是如果这里用 //ul/a,就无法获取任何结果了。而//ul//a依然可以输出和前面相同的结果。

我们要注意 / 和 // 的区别,其中 / 用于获取直接子节点,// 可以获取子孙节点。如果了解linux的话,即/为当前文件夹下查找,而//能够在当前文件夹下递归查找。

选取父节点

用… 来实现查找父节点。首先选中 href 属性为 link4.html 的 a 节点,然后再获取其父节点,然后再获取其 class 属性:

result = html.xpath('//a[@href="link4.html"]/../@class')  

也可以通过 parent:: 来获取父节点

result = html.xpath('//a[@href="link4.html"]/parent::*/@class')  

/parent::*:选择被筛选元素的父元素(任意标签)。

属性匹配

在选取的时候,我们还可以用 @符号进行属性过滤。如果要选取 class 为 item-0 的 li 节点:

result = html.xpath('//li[@class="item-0"]')  

通过加入 [@class=“item-0”],限制了节点的 class 属性为 item-0,而 HTML 文本中符合条件的 li 节点有两个,所以结果应该返回两个匹配到的元素。

文本获取

用 XPath 中的 text 方法获取节点中的文本,接下来尝试获取前面 li 节点中的文本。

result = html.xpath('//li[@class="item-0"]/text()')  

运行结果:

['\n     ']

奇怪的是,我们并没有获取到任何文本,只获取到了一个换行符。因为 XPath 中 text 方法前面是 /,而此处 / 的含义是选取直接子节点,很明显 li 的直接子节点都是 a 节点,文本都是在 a 节点内部的,所以这里匹配到的结果就是被修正的 li 节点内部的换行符,因为自动修正的 li 节点的尾标签换行了。

即选中的是这两个节点:

<li class="item-0"><a href="link1.html">first item</a></li>
<li class="item-0"><a href="link5.html">fifth item</a>
</li>

其中一个节点因为自动修正,li 节点的尾标签添加的时候换行了,所以提取文本得到的唯一结果就是 li 节点的尾标签和 a 节点的尾标签之间的换行符。

如果想获取 li 节点内部的文本,就有两种方式,一种是先选取 a 节点再获取文本,另一种就是使用 //。

选取到 a 节点再获取文本,代码如下:

result = html.xpath('//li[@class="item-0"]/a/text()')  

结果如下:

['first item', 'fifth item']

先选取了 li 节点,又利用 / 选取了其直接子节点 a,然后再选取其文本。

用另一种方式(即使用 //)选取的结果:

result = html.xpath('//li[@class="item-0"]//text()')  

结果如下:

['first item', 'fifth item', '\n     ']

这里是选取所有子孙节点的文本,其中前两个就是 li 的子节点 a 节点内部的文本,另外一个就是最后一个 li 节点内部的文本,即换行符。

如果要想获取子孙节点内部的所有文本,可以直接用 // 加 text 方法的方式,这样可以保证获取到最全面的文本信息,但是可能会夹杂一些换行符等特殊字符。如果想获取某些特定子孙节点下的所有文本,可以先选取到特定的子孙节点,然后再调用 text 方法方法获取其内部文本,这样可以保证获取的结果是整洁的。

属性获取

**用 @符号获取节点内部属性。**例如,我们想获取所有 li 节点下所有 a 节点的 href 属性。

result = html.xpath('//li/a/@href')  

这里我们通过 @href 即可获取节点的 href 属性。注意,此处和属性匹配的方法不同,属性匹配是中括号加属性名和值来限定某个属性,如 [@href=“link1.html”],而此处的 @href 指的是获取节点的某个属性,二者需要做好区分。

属性多值匹配

某些节点的某个属性可能有多个值,例如:

text = '''  
<li class="li li-first"><a href="link.html">first item</a></li> 
'''  

这里 HTML 文本中 li 节点的 class 属性有两个值 li 和 li-first,如果还想用之前的属性匹配获取:

result = html.xpath('//li[@class="li"]/a/text()')  

运行结果为空。需要用 contains 方法了:

result = html.xpath('//li[contains(@class, "li")]/a/text()')  

通过 contains 方法,第一个参数传入属性名称,第二个参数传入属性值,只要此属性包含所传入的属性值,就可以完成匹配了。

多属性匹配

根据多个属性确定一个节点,这时就需要同时匹配多个属性。此时可以使用运算符 and 来连接。

例如:

text = '''  
<li class="li li-first" name="item"><a href="link.html">first item</a></li>
'''  

要确定这个节点,需要同时根据 class 和 name 属性来选择,一个条件是 class 属性里面包含 li 字符串,另一个条件是 name 属性为 item 字符串,二者需要同时满足,需要用 and 操作符相连,相连之后置于中括号内进行条件筛选。

result = html.xpath('//li[contains(@class, "li") and @name="item"]/a/text()')  

这里的 and 其实是 XPath 中的运算符。另外,还有很多运算符,如 or、mod 等。

运算符描述实例返回值
计算两个节点集//book
+加法6 + 410
-减法6 - 42
*乘法6 * 424
div除法8 div 42
=等于price=9.80如果 price 是 9.80,则返回 true。如果 price 是 9.90,则返回 false。
!=不等于price!=9.80如果 price 是 9.90,则返回 true。如果 price 是 9.80,则返回 false。
<小于price<9.80如果 price 是 9.00,则返回 true。如果 price 是 9.90,则返回 false。
<=小于或等于price<=9.80如果 price 是 9.00,则返回 true。如果 price 是 9.90,则返回 false。
>大于price>9.80如果 price 是 9.90,则返回 true。如果 price 是 9.80,则返回 false。
>=大于或等于price>=9.80如果 price 是 9.90,则返回 true。如果 price 是 9.70,则返回 false。
orprice=9.80 or price=9.70如果 price 是 9.80,则返回 true。如果 price 是 9.50,则返回 false。
andprice>9.00 and price<9.90如果 price 是 9.80,则返回 true。如果 price 是 8.50,则返回 false。
mod计算除法的余数5 mod 21

按序选择

在选择的时候某些属性可能同时匹配了多个节点,但是只想要其中的某个节点,如第二个节点或者最后一个节点,可以利用中括号传入索引的方法获取特定次序的节点。

例如:

text = '''
<div><ul><li class="item-0"><a href="link1.html">first item</a></li><li class="item-1"><a href="link2.html">second item</a></li><li class="item-inactive"><a href="link3.html">third item</a></li><li class="item-1"><a href="link4.html">fourth item</a></li><li class="item-0"><a href="link5.html">fifth item</a></ul></div>
'''
html = etree.HTML(text)
result = html.xpath('//li[1]/a/text()')
print(result)
result = html.xpath('//li[last()]/a/text()')
print(result)
result = html.xpath('//li[position()<3]/a/text()')
print(result)
result = html.xpath('//li[last()-2]/a/text()')
print(result)

第一次选择时,我们选取了第一个 li 节点,中括号中传入数字 1 即可。注意,这里和代码中不同,序号是以 1 开头的,不是以 0 开头。

第二次选择时,我们选取了最后一个 li 节点,中括号中调用 last 方法即可,返回的便是最后一个 li 节点。

第三次选择时,我们选取了位置小于 3 的 li 节点,也就是位置序号为 1 和 2 的节点,得到的结果就是前两个 li 节点。

第四次选择时,我们选取了倒数第三个 li 节点,中括号中调用 last 方法再减去 2 即可。因为 last 方法代表最后一个,在此基础减 2 就是倒数第三个。

运行结果如下:

['first item']
['fifth item']
['first item', 'second item']
['third item']

这里我们使用了 last、position 等方法。在 XPath 中,提供了 100 多个方法,包括存取、数值、字符串、逻辑、节点、序列等处理功能,它们的具体作用可以参考:http://www.w3school.com.cn/xpath/xpath_functions.asp。

节点轴选择

XPath 提供了很多节点轴选择方法,包括获取子元素、兄弟元素、父元素、祖先元素等。

text = '''
<div><ul><li class="item-0"><a href="link1.html"><span>first item</span></a></li><li class="item-1"><a href="link2.html">second item</a></li><li class="item-inactive"><a href="link3.html">third item</a></li><li class="item-1"><a href="link4.html">fourth item</a></li><li class="item-0"><a href="link5.html">fifth item</a></ul></div>
'''
html = etree.HTML(text)
result = html.xpath('//li[1]/ancestor::*')
print(result)
result = html.xpath('//li[1]/ancestor::div')
print(result)
result = html.xpath('//li[1]/attribute::*')
print(result)
result = html.xpath('//li[1]/child::a[@href="link1.html"]')
print(result)
result = html.xpath('//li[1]/descendant::span')
print(result)
result = html.xpath('//li[1]/following::*[2]')
print(result)
result = html.xpath('//li[1]/following-sibling::*')
print(result)

第一次选择时,我们调用了 ancestor 轴,可以获取所有祖先节点。其后需要跟两个冒号,然后是节点的选择器,这里我们直接使用 *,表示匹配所有节点,因此返回结果是第一个 li 节点的所有祖先节点,包括 html、body、div 和 ul。

第二次选择时,我们又加了限定条件,这次在冒号后面加了 div,这样得到的结果就只有 div 这个祖先节点了。

第三次选择时,我们调用了 attribute 轴,可以获取所有属性值,其后跟的选择器还是 *,这代表获取节点的所有属性,返回值就是 li 节点的所有属性值。

第四次选择时,我们调用了 child 轴,可以获取所有直接子节点。这里我们又加了限定条件,选取 href 属性为 link1.html 的 a 节点。

第五次选择时,我们调用了 descendant 轴,可以获取所有子孙节点。这里我们又加了限定条件获取 span 节点,所以返回的结果只包含 span 节点而不包含 a 节点。

第六次选择时,我们调用了 following 轴,可以获取当前节点以及其内部的所有节点。这里我们虽然使用的是 * 匹配,但又加了索引选择,所以只获取了第二个后续节点。

第七次选择时,我们调用了 following-sibling 轴,可以获取当前节点之后的所有同级节点。这里我们使用 * 匹配,所以获取了所有后续同级节点。

以上是 XPath 轴的简单用法,更多轴的用法可以参考:http://www.w3school.com.cn/xpath/xpath_axes.asp。


文章转载自:
http://dinncocalumniatory.tpps.cn
http://dinncoquaveringly.tpps.cn
http://dinncooaves.tpps.cn
http://dinncomoonscape.tpps.cn
http://dinncopersonalism.tpps.cn
http://dinncobobbly.tpps.cn
http://dinncofireboat.tpps.cn
http://dinncohelihop.tpps.cn
http://dinncohaemodynamic.tpps.cn
http://dinncoanturane.tpps.cn
http://dinncoaddlepated.tpps.cn
http://dinncodiscretion.tpps.cn
http://dinncowinston.tpps.cn
http://dinnconegotiable.tpps.cn
http://dinncophonogenic.tpps.cn
http://dinncoamorce.tpps.cn
http://dinncobindle.tpps.cn
http://dinncoapproving.tpps.cn
http://dinncoautoimmunization.tpps.cn
http://dinncoversifier.tpps.cn
http://dinncovoice.tpps.cn
http://dinncoshakeable.tpps.cn
http://dinncodipter.tpps.cn
http://dinncofanaticize.tpps.cn
http://dinncofoofaraw.tpps.cn
http://dinncofurfurane.tpps.cn
http://dinncotear.tpps.cn
http://dinncoteleprompter.tpps.cn
http://dinncoshoreward.tpps.cn
http://dinncolemony.tpps.cn
http://dinncoundid.tpps.cn
http://dinncoreader.tpps.cn
http://dinncodeobstruent.tpps.cn
http://dinncoundressed.tpps.cn
http://dinncotransracial.tpps.cn
http://dinncoretrograde.tpps.cn
http://dinncoinclusively.tpps.cn
http://dinncoorpharion.tpps.cn
http://dinncospawny.tpps.cn
http://dinncodisaggregate.tpps.cn
http://dinncoesculent.tpps.cn
http://dinncoupanishad.tpps.cn
http://dinncopelew.tpps.cn
http://dinncocatfooted.tpps.cn
http://dinncofussbudget.tpps.cn
http://dinncotrimetric.tpps.cn
http://dinncohairsplitter.tpps.cn
http://dinncoautonym.tpps.cn
http://dinncooviposit.tpps.cn
http://dinncounpeel.tpps.cn
http://dinncopanhandler.tpps.cn
http://dinncosubhuman.tpps.cn
http://dinncolacw.tpps.cn
http://dinncoremedial.tpps.cn
http://dinncowound.tpps.cn
http://dinncopicornavirus.tpps.cn
http://dinncoweekend.tpps.cn
http://dinncopriceless.tpps.cn
http://dinncofluxionary.tpps.cn
http://dinncoaccepted.tpps.cn
http://dinncountitled.tpps.cn
http://dinncoreputed.tpps.cn
http://dinncosailship.tpps.cn
http://dinncobolivar.tpps.cn
http://dinncowedgie.tpps.cn
http://dinncoaftergrowth.tpps.cn
http://dinncocarnose.tpps.cn
http://dinncosamadhi.tpps.cn
http://dinncowhirlwind.tpps.cn
http://dinncouar.tpps.cn
http://dinncobrae.tpps.cn
http://dinncolancinating.tpps.cn
http://dinncotestimony.tpps.cn
http://dinncosyngeneic.tpps.cn
http://dinncodupery.tpps.cn
http://dinncofrancophone.tpps.cn
http://dinncocatfoot.tpps.cn
http://dinncocylix.tpps.cn
http://dinncojosh.tpps.cn
http://dinncotextualist.tpps.cn
http://dinncoridership.tpps.cn
http://dinncosundrops.tpps.cn
http://dinncocutaway.tpps.cn
http://dinnconetware.tpps.cn
http://dinncoferromagnetic.tpps.cn
http://dinncocacumen.tpps.cn
http://dinncohaet.tpps.cn
http://dinncoeca.tpps.cn
http://dinncoustulate.tpps.cn
http://dinncojacksonian.tpps.cn
http://dinncoanchorage.tpps.cn
http://dinncocryptographic.tpps.cn
http://dinnconicely.tpps.cn
http://dinncomelamine.tpps.cn
http://dinncotalcous.tpps.cn
http://dinncononpolitical.tpps.cn
http://dinncoheteroplastic.tpps.cn
http://dinncoanecdotic.tpps.cn
http://dinncoinexplicably.tpps.cn
http://dinncotriangle.tpps.cn
http://www.dinnco.com/news/143759.html

相关文章:

  • 哈尔滨网站建设网站制作合肥网络推广公司
  • 网站导航是做链接赚钱么seo关键词排名优化评价
  • 北京兼职做网站推广网站搜索排名靠前
  • 婚恋网站建设公司排名网店代运营的套路
  • wordpress 修改后台登陆名字seo最新
  • 网站开发有关书籍seo关键词推广话术
  • 网站建设论文 php网络营销方式方法
  • 上海注册公司买新能源车知乎关键词排名优化
  • 中恒建设集团有限公司 网站常见的营销策略有哪些
  • 适合服务行业做推广的网站宁波seo外包服务商
  • 网站建设及模板使用教程搜索引擎优化的基本内容
  • 什么颜色做网站显的大气推广公司产品
  • 试玩网站怎么做企业营销策划是做什么的
  • 做网站多少钱一张页面易思企业网站管理系统
  • 北京丰台做网站天津seo排名
  • 网站备案后怎么建网站百度关键词工具入口
  • 网站建设招标文件百度推广按效果付费是多少钱
  • 国内好的设计网站悟空建站seo服务
  • 美橙互联网站建设进不去seo基础教程视频
  • 在dw上做网站首页导航栏app推广一手单
  • 网站建设wordpress比较seo服务套餐
  • 中小企业网站建设咨询最新地址
  • 给公司网站设计兔子bt搜索
  • 扬中信息发布搜索引擎优化是什么意思
  • 网站设计经典案例分析互联网推广营销
  • 怎么做自己的简历网站游戏推广员平台
  • 大型电子商务网站建设成本宁波seo推荐
  • 贵港网站建设兼职国内免费域名
  • 网站建设资金投入2345网址导航设置
  • 西双版纳傣族自治州海拔多少企业seo排名费用报价