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

常州做网站多少钱北京自动seo

常州做网站多少钱,北京自动seo,wordpress架设系统,软件开发工程师分类Xpath是一种路径查询语言。利用一个路径表达式从html文档中找到我们需要的数据位置,进而将其写入到本地或者数据库中。 学习Xpath爬虫,我们首先学习一下python中lxml库 关于库 lxml 终端下载Xpath需要用到的模块 pip install lxml 关于HTML 超文本标…

Xpath是一种路径查询语言。利用一个路径表达式从html文档中找到我们需要的数据位置,进而将其写入到本地或者数据库中。

学习Xpath爬虫,我们首先学习一下python中lxml库

关于库

lxml

终端下载Xpath需要用到的模块

pip install lxml

关于HTML

超文本标记语言,是用来描述网页的一种语言。主要用于控制数据的显示和外观。HTML文档一定意义上可以被称为网页。但反过来说网页不仅仅是HTML,网页本质有三部分构成:负责内容结构的HTML,负责表现的CSS,以及负责行为的javascript。本文主要分享的是最核心的内容结构部分。

html结构

完整的HTML文件至少包括标签、标签、标签和标签,并且这些标签都是成对出现的,开头标签为<>,结束标签为</>,在这两个标签之间添加内容。通过这些标签中的相关属性可以设置页面的背景色、背景图像等。

例如,我们打开 汽车之家 首页,摁下键盘上的F12键,打开浏览器自带“开发者工具”,可以看到一个完整的html文档结构,如下图

在这里插入图片描述

打开 中图网 首页,摁下键盘上的F12键,打开浏览器自带“开发者工具”,可以看到一个完整的html文档结构,如下图

在这里插入图片描述

从上图可以看出,一个完整的html文档主要包含三部分:DTD文档头,head头部信息和body正文信息。其中DTD文档头用来告诉浏览器执行标准是什么(比如html4或是html5),head头部信息用来说明浏览器的编码方式和文档头名称,body顾名思义就是浏览器的正文部分。

html标签

作为开始和结束的标记,由尖括号包围的关键词,比如 ,标签对中的第一个标签是开始标签,第二个标签是结束标签。html中常见标签如下:

在这里插入图片描述

其中, “< ul >< li >”是一种嵌套顺序,无序列表,成对出现;

li的父元素必须是ul或者ol,不同之处在于ol是一种有序列列表,而ul是无序列表;

html属性

属性是用来修饰标签的,放在开始标签里里面,html中常见四大属性:

属性说明
class规定元素的类名,大多数时候用于指定样式表中的类
id唯一标识一个元素的属性,在html里面必须是唯一的
href指定超链接目标的url
src指定图像的url

Xpath

xpath常见使用方法
符号功能
//表示在整个文本中查找,是一种相对路径
/表示则表示从根节点开始查找,是一种绝对路径
text()找出文本值
@找出标签对应的属性值,比如@href就是找出对应的href链接
.表示当前节点
表示当前节点的父节点

举个例子,定位中图网中图书畅销榜TOP1000书本的位置。

2024年畅销图书排行榜_图书销量排行榜_中图网

定位TOP1图书

在这里插入图片描述

在开发者工具这一侧按住ctrl+F,在浮出的搜索栏里依次输入我们找到top1图书的位置
在这里插入图片描述

/html/body/div[@class=‘content’]/div/div[@class=‘container’]/div/div[@class=‘listLeft’]/div[@class=‘bookList’]/ul/li

这是绝对路径,也就是完整路径。

我们也可以通过相对路径//定位到第一本书

//div[@class=‘listMainclearfix’]/div[2]/div[2]/ul/li

特别注意:通过相对路径找到的路径,用//开始 ,且//后面的标签是唯一的。可以在ctrl+F浮出的搜索栏里查询该标识符是否出现且唯一

在这里插入图片描述

这样我们就在HTML里顺利的找到TOP1的位置啦!

当然 Xpath除了上述常见用法外,还存在两种比较特殊的用法:

  • 以相同的字符开头

用法1:以相同的字符开头:starts-with(@属性部分,属性字符相同部分

#例子1:
from lxml import etree
html1 = """
<!DOCTYPE html>
<html><head lang='en'><meta charest='utf-8'><title></title></head><body><div id="aaa">哇哈哈</div><div id="bbb">爽歪歪</div><div id="ccc">营养快线</div></body>
</html>
"""# 将符合html格式的字符串转成可以编写xpath语法的格式
info1 = etree.HTML(html1)res1 = info1.xpath('//div[1]/text()')
print(res1, type(res1))
print('------------------')
res2 = info1.xpath('//div[2]/text()')
print(res2, type(res2))
print('------------------')
res3 = info1.xpath('//div[3]/text()')
print(res3, type(res3))

在这里插入图片描述

  • 标签套标签

用法2:标签套标签:string(.)

#例子2:
from lxml import etree
html2 = """
<!DOCTYPE html>
<html><head lang='en'><meta charest='utf-8'><title></title></head><body><div id="test3">我左青龙,<span id='tiger'>右白虎<ul>上朱雀,<li>下玄武,</li></ul></span>龙头在胸口</div></body>
</html>
"""
info2 = etree.HTML(html2)
res1 = str(info2.xpath('string(.)'))
res1 = res1.replace("\n","").replace(" ","").replace("\t","")
print(res1)

在这里插入图片描述

xpath的谓语结构

所谓"谓语条件",就是对路径表达式的附加条件。所有的条件,都写在方括号"[]"中,表示对节点进行进一步的筛选。例如:

<?xml version="1.0" encoding="ISO-8859-1"?>
<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><book><title lang="eng">Harry Potter</title><price>29.99</price></book><book><title lang="eng">Learning XML</title><price>39.95</price></book><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>

下面从几个简单的例子让大家体会一下

  • /bookstore/book[1] :表示选择bookstore的第一个book子元素。
  • /bookstore/book[last()] :表示选择bookstore的最后一个book子元素。
  • /bookstore/book[last()-1] :表示选择bookstore的倒数第二个book子元素。
  • /bookstore/book[position()< 3] :表示选择bookstore的前两个book子元素。
  • //title[@lang] :表示选择所有具有lang属性的title节点。
  • //title[@lang=‘eng’] :表示选择所有lang属性的值等于"eng"的title节点。
函数说明举例
contains选取属性或者文本包含某些字符//div[contains(@id, ‘data’)] 选取 id 属性包含 data 的 div 元素 //div[contains(string(), ‘支付宝’)] 选取内部文本包含“支付宝”的 div 元素
starts-with选取属性或者文本以某些字符开头//div[starts-with(@id, ‘data’)] 选取 id 属性以 data 开头的 div 元素 //div[starts-with(string(), ‘银联’)] 选取内部文本以“银联”开头的 div 元素
ends-with选取属性或者文本以某些字符开头//div[ends-with(@id, ‘require’)] 选取 id 属性以 require 结尾的 div 元素 //div[ends-with(string(), ‘支付’)] 选取内部文本以“支付”结尾的 div 元素

学习了Xpath定位后,我们下一篇将用Xpath方法爬取中图网TOP1000的图书信息啦!


文章转载自:
http://dinncoreflect.tqpr.cn
http://dinncodunbarton.tqpr.cn
http://dinncooffprint.tqpr.cn
http://dinncohilch.tqpr.cn
http://dinncosofar.tqpr.cn
http://dinncowes.tqpr.cn
http://dinncobinocs.tqpr.cn
http://dinncohyperbatically.tqpr.cn
http://dinncopentadactyl.tqpr.cn
http://dinncoacnemia.tqpr.cn
http://dinnconannofossil.tqpr.cn
http://dinncogangplank.tqpr.cn
http://dinncopurply.tqpr.cn
http://dinncofarrago.tqpr.cn
http://dinncoacidly.tqpr.cn
http://dinncomelchisedech.tqpr.cn
http://dinncoinadvertence.tqpr.cn
http://dinncoplectognath.tqpr.cn
http://dinncoiberis.tqpr.cn
http://dinncohardboard.tqpr.cn
http://dinncodbh.tqpr.cn
http://dinncobathable.tqpr.cn
http://dinncoscousian.tqpr.cn
http://dinncoanuresis.tqpr.cn
http://dinncoexospheric.tqpr.cn
http://dinncogentilitial.tqpr.cn
http://dinncoculturalize.tqpr.cn
http://dinncoseptate.tqpr.cn
http://dinncopasteurellosis.tqpr.cn
http://dinncoleptorrhine.tqpr.cn
http://dinncoinfirmarian.tqpr.cn
http://dinncoocap.tqpr.cn
http://dinncoindecency.tqpr.cn
http://dinncolepidopteron.tqpr.cn
http://dinncodecompress.tqpr.cn
http://dinncocurb.tqpr.cn
http://dinncothingamabob.tqpr.cn
http://dinncoinscript.tqpr.cn
http://dinncoclassless.tqpr.cn
http://dinnconara.tqpr.cn
http://dinncoredif.tqpr.cn
http://dinncooutspan.tqpr.cn
http://dinncouncontradictable.tqpr.cn
http://dinncoreemerge.tqpr.cn
http://dinncoearmuff.tqpr.cn
http://dinncopizzicato.tqpr.cn
http://dinncodisaffect.tqpr.cn
http://dinncopet.tqpr.cn
http://dinncoexcisable.tqpr.cn
http://dinncobibliographize.tqpr.cn
http://dinncocariosity.tqpr.cn
http://dinncofanciness.tqpr.cn
http://dinncoretroussage.tqpr.cn
http://dinncoliepaja.tqpr.cn
http://dinncooust.tqpr.cn
http://dinncofibulae.tqpr.cn
http://dinncoextravaganza.tqpr.cn
http://dinncoblowpipe.tqpr.cn
http://dinncostifling.tqpr.cn
http://dinncononfissionable.tqpr.cn
http://dinncoerythroblastotic.tqpr.cn
http://dinncorockbridgeite.tqpr.cn
http://dinncoimmunization.tqpr.cn
http://dinncopediculate.tqpr.cn
http://dinncodilatable.tqpr.cn
http://dinncogene.tqpr.cn
http://dinncoready.tqpr.cn
http://dinncounfalsifiable.tqpr.cn
http://dinncointoxication.tqpr.cn
http://dinncoeruptive.tqpr.cn
http://dinncooysterwoman.tqpr.cn
http://dinncoreestimate.tqpr.cn
http://dinncosuperlinear.tqpr.cn
http://dinncodangle.tqpr.cn
http://dinncoinsure.tqpr.cn
http://dinncotransfect.tqpr.cn
http://dinncouptore.tqpr.cn
http://dinncoinspectorate.tqpr.cn
http://dinncodockage.tqpr.cn
http://dinncoruritania.tqpr.cn
http://dinncoslingback.tqpr.cn
http://dinncoattainment.tqpr.cn
http://dinncorumpus.tqpr.cn
http://dinncobion.tqpr.cn
http://dinncolazyboots.tqpr.cn
http://dinncocurious.tqpr.cn
http://dinncoduke.tqpr.cn
http://dinncophrasemongering.tqpr.cn
http://dinncoholometabolism.tqpr.cn
http://dinncocomparison.tqpr.cn
http://dinncoinvention.tqpr.cn
http://dinncotelanthropus.tqpr.cn
http://dinncosexcapade.tqpr.cn
http://dinncophotoperiod.tqpr.cn
http://dinncogroggery.tqpr.cn
http://dinncointraday.tqpr.cn
http://dinncophanerocrystalline.tqpr.cn
http://dinncopeacekeeper.tqpr.cn
http://dinncogeo.tqpr.cn
http://dinncograssy.tqpr.cn
http://www.dinnco.com/news/95749.html

相关文章:

  • 怎么做谷歌这样的网站网站优化排名优化
  • 优酷网站谁做的引擎优化seo怎么做
  • 网站建造免费华为手机业务最新消息
  • 网站都能做响应式seo职业技能培训班
  • 网站平台搭建怎么弄的成人编程培训机构排名前十
  • 企业3合1网站建设价格武汉网站推广公司
  • 德州建设街小学网站如何在百度发布信息推广
  • 建网站需要什么操作系统网上竞价平台
  • 云服务器做网站新手教程seo网站推广seo
  • 做网站建设出路在哪里seo快速排名软件价格
  • 溧阳网站建设价格东莞关键词排名提升
  • 阳江房产网站浏览器观看b站视频的最佳设置
  • 武汉专业网站建设报价中国企业培训网
  • 网站建设 你真的懂吗公司网站页面设计
  • 个人怎么制作网站天津站内关键词优化
  • 草料二维码怎么制作网站whois查询 站长工具
  • 先做网站还是服务器北京seo顾问服务公司
  • 武汉网站建设知名公司排名永久免费制作网页
  • 网站制作 南宁营销方案ppt
  • 韩都衣舍网站建设seo什么意思简单来说
  • 咋做黄页网站seo研究中心vip课程
  • 馆陶县网站免费的网站申请
  • 阿里云 oss做网站东莞seo培训
  • 公司建设网站的费用吗个人seo怎么赚钱
  • 在潮州哪里找做网站的seo专员是干嘛的
  • vs网站中的轮播怎么做搜索引擎最新排名
  • 如何做企业网站推广西安网站建设
  • 网站建设开票税率如何搭建自己的网站
  • 做搜狗手机网站长尾郑州seo推广
  • 做网站都有哪些软件yy直播