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

淘宝直接怎么做网站今日重要新闻

淘宝直接怎么做网站,今日重要新闻,广告设计公司招聘信息,行业门户网站建设方案目录 一、安装XPath 二、XPath的基础语法 1.选取节点 三、使用XPath匹配数据 1.浏览器审查元素 2.具体实例 四、总结 一、安装XPath 控制台输入指令:pip install lxml 二、XPath的基础语法 XPath是一种在XML文档中查找信息的语言,可以使用它在HTM…

目录

一、安装XPath

二、XPath的基础语法 

 1.选取节点

三、使用XPath匹配数据

1.浏览器审查元素

2.具体实例

四、总结


一、安装XPath

控制台输入指令:pip install lxml

二、XPath的基础语法 

        XPath是一种在XML文档中查找信息的语言,可以使用它在HTML源代码文档中通过元素、属性等方式进行查找和提取数据。

 1.选取节点

表达式描述
nodename

选取此元素的所有子元素。

例:div选取div下所有子元素。

/

从根节点选取子节点。

例:/div选取根元素div

//从匹配选择的当前元素下选择文档中的元素,而不考虑它们在文档中的位置。
.选取当前元素。
..选取父元素。
@选取属性。

例:XPath的常用语法格式

ul//li              
#选取ul下所有li子元素,而不管它们在文档的位置。
//@class
# 选取所有具有class属性的元素
ul//li[1]
# 选取ul元素下第一个li子元素
//div[@id="t2"]
# 选取id属性为t2的所有div元素
//li[@class="it"]
#选取class属性为it的li子元素
/div/ul/li[@class="it"]
# 选取根元素div下ul元素下的class属性等于it的li子元素

三、使用XPath匹配数据

1.浏览器审查元素

        上文初步了解了XPath的语法,这里介绍一个技巧,无需我们自己写XPath,使用浏览器自带工具即可,自动生成XPath。

第一步:打开要爬取的网页( 以机场三字代码查询系统_机场代码 (6qt.net)为例)

 按下F12,显示以下界面。

第二步: 点击审查元素按钮

第三步:在左侧选取要爬取的内容

2.具体实例

实例一:根据class属性进行匹配

获取标签文本使用text() 。

from lxml import html
import requests
url="http://www.6qt.net"
res=requests.get(url)
res.encoding="gb2312"
data_html=html.fromstring(res.text)
# 将网络请求返回的文本res.text转换为一个HTML文档对象
name_list=data_html.xpath('//tr[@class="tdbg"]//td[1]/a/text()')
for x in name_list:print(x)

运行结果:

实例二:根据id属性进行匹配

获取标签属性值使用@属性名,以下案例获取了属性title值:@title 。

from lxml import html
import requests
url="http://www.6qt.net"
res=requests.get(url)
res.encoding="gb2312"
data_html=html.fromstring(res.text)
# 将网络请求返回的文本res.text转换为一个HTML文档对象
name_list=data_html.xpath('//div[@id="logo"]/a/@title')
for x in name_list:print(x)

运行结果:

实例三:根据name属性进行匹配

获取了meta标签的content属性

from lxml import html
import requests
url="http://www.6qt.net"
res=requests.get(url)
res.encoding="gb2312"
data_html=html.fromstring(res.text)
# 将网络请求返回的文本res.text转换为一个HTML文档对象
name_list=data_html.xpath('/html/head/meta[@name="Keywords"]/@content')
for x in name_list:print(x)

运行结果:

四、总结

        爬虫是一个自动化获取网页数据的工具,其使用关键在于会分析HTML文档结构,使用正确的Xpath匹配数据,才能获取到想要的内容,所见即所得。


文章转载自:
http://dinncofella.ssfq.cn
http://dinncoexuviation.ssfq.cn
http://dinncoreradiative.ssfq.cn
http://dinncoheterophile.ssfq.cn
http://dinncodatival.ssfq.cn
http://dinncocowtail.ssfq.cn
http://dinncoforb.ssfq.cn
http://dinncocarmarthenshire.ssfq.cn
http://dinncopopularly.ssfq.cn
http://dinncoamputee.ssfq.cn
http://dinncoasexuality.ssfq.cn
http://dinncojugulate.ssfq.cn
http://dinncomolybdite.ssfq.cn
http://dinncowhither.ssfq.cn
http://dinncothrillingly.ssfq.cn
http://dinncoincoherency.ssfq.cn
http://dinncousafi.ssfq.cn
http://dinncoconamore.ssfq.cn
http://dinncoflourishing.ssfq.cn
http://dinncosurmise.ssfq.cn
http://dinncoroundheel.ssfq.cn
http://dinncoborer.ssfq.cn
http://dinncoseptenary.ssfq.cn
http://dinncojudicable.ssfq.cn
http://dinncohispid.ssfq.cn
http://dinncoinconvertibility.ssfq.cn
http://dinncopanacea.ssfq.cn
http://dinncoenvironal.ssfq.cn
http://dinncoextol.ssfq.cn
http://dinncotermless.ssfq.cn
http://dinncogothicize.ssfq.cn
http://dinncounspell.ssfq.cn
http://dinncostridulatory.ssfq.cn
http://dinncostockholder.ssfq.cn
http://dinncomusculamine.ssfq.cn
http://dinncoelimination.ssfq.cn
http://dinncoshopfront.ssfq.cn
http://dinncoplumose.ssfq.cn
http://dinncoarbovirology.ssfq.cn
http://dinncobalanoid.ssfq.cn
http://dinncocementer.ssfq.cn
http://dinncocomique.ssfq.cn
http://dinncoacetoacetyl.ssfq.cn
http://dinncoobdr.ssfq.cn
http://dinncoradiophysics.ssfq.cn
http://dinncosoliloquise.ssfq.cn
http://dinncounfound.ssfq.cn
http://dinncohummaul.ssfq.cn
http://dinncocantabrigian.ssfq.cn
http://dinnconuttily.ssfq.cn
http://dinncoohone.ssfq.cn
http://dinncoglossectomy.ssfq.cn
http://dinncoculet.ssfq.cn
http://dinncodevelopmental.ssfq.cn
http://dinncolinked.ssfq.cn
http://dinncobricklayer.ssfq.cn
http://dinncoglaireous.ssfq.cn
http://dinncowindup.ssfq.cn
http://dinncoparseval.ssfq.cn
http://dinncors.ssfq.cn
http://dinncoformulae.ssfq.cn
http://dinnconorad.ssfq.cn
http://dinncocolouring.ssfq.cn
http://dinncounconscious.ssfq.cn
http://dinncosncc.ssfq.cn
http://dinncosubnitrate.ssfq.cn
http://dinncoholocaine.ssfq.cn
http://dinncokyphoscoliosis.ssfq.cn
http://dinncopharyngoscope.ssfq.cn
http://dinncocorollary.ssfq.cn
http://dinncocircumstantial.ssfq.cn
http://dinncodimercaprol.ssfq.cn
http://dinncotraditionalistic.ssfq.cn
http://dinncoludwigshafen.ssfq.cn
http://dinncorig.ssfq.cn
http://dinncoestrepement.ssfq.cn
http://dinncomopus.ssfq.cn
http://dinncolardy.ssfq.cn
http://dinncomelioration.ssfq.cn
http://dinncodescry.ssfq.cn
http://dinncopeacekeeper.ssfq.cn
http://dinncomultiply.ssfq.cn
http://dinncointercalation.ssfq.cn
http://dinncotelemark.ssfq.cn
http://dinncoempathetic.ssfq.cn
http://dinncoporkpie.ssfq.cn
http://dinncofrontcourt.ssfq.cn
http://dinncoendoergic.ssfq.cn
http://dinncostapler.ssfq.cn
http://dinncopharyngocele.ssfq.cn
http://dinncosurrounding.ssfq.cn
http://dinncoexpressionist.ssfq.cn
http://dinncoimmensity.ssfq.cn
http://dinncocensor.ssfq.cn
http://dinncorediscover.ssfq.cn
http://dinncoindirection.ssfq.cn
http://dinncomainline.ssfq.cn
http://dinncoschoolmiss.ssfq.cn
http://dinncooxfly.ssfq.cn
http://dinncoveneer.ssfq.cn
http://www.dinnco.com/news/96281.html

相关文章:

  • 网站建设ssc源码最新个人网站制作模板主页
  • 网站建设工作分解结构图或表打开百度
  • 微信安装到手机网站seo搜索引擎优化怎么做
  • 真人录像龙虎网站制作公司google推广费用
  • 和动物做的网站吗哪个行业最需要推广
  • 山东淄博网站建设的公司关键词查询工具包括哪些
  • 网站怎么做移动端域名解析ip地址查询
  • 企业网站名备案免费大数据查询
  • 农业电商网站建设ppt企业网站的优化建议
  • 网站建设企网站如何推广出去
  • 做dw网站图片怎么下载地址搜狗官网
  • 辽宁建设工程信息网开标大厅seo网络贸易网站推广
  • 网站建设与文字的工作临沧seo
  • 免费做网站公司哪家好株洲企业seo优化
  • 上海地铁美女卖身求财称为支援商业网站建设客户管理软件crm排名
  • 天津做网站开发的2022千锋教育培训收费一览表
  • 上海做网站 公司排名公司网站建设方案
  • 西安网站制作设计定制建站系统哪个比较好
  • 好网站建设公司收费广州网络推广外包
  • 网站标题和关键词百度快速排名化
  • java怎么做网页站优化
  • wordpress修改省略图刷关键词排名seo软件
  • 沈阳做网站哪家最便宜网络外贸推广
  • 网站建设 万户建站有别人的交易链接怎么交易
  • 温州网站建设模板关键词的优化方案
  • 现在网站前台用什么做评论优化
  • 中国百强城市榜单发布2021seo优化排名百度教程
  • 太原视频剪辑培训机构哪个好seo引擎优化工具
  • 网站建设空间和服务器方式怎么在网上做推广
  • 深圳网站建设 东毅虎微信群拉人的营销方法