浚县网站建设,搭建网站需要什么技术,wordpress婚纱摄影主题,快手秒刷自助网站第一章XML
一、xml简介
1.什么是XML#xff1f;
1#xff0c;XML指可扩展标记语言
2#xff0c;XML是一种标记语言#xff0c;类似于HTML
3#xff0c;XML的设计宗旨是传输数据#xff0c;而非显示数据
4#xff0c;XML标签需要我们自己自定义
5#xff0c;XML被…第一章XML
一、xml简介
1.什么是XML
1XML指可扩展标记语言
2XML是一种标记语言类似于HTML
3XML的设计宗旨是传输数据而非显示数据
4XML标签需要我们自己自定义
5XML被设计为具有自我描述性
2.XML和HTML的区别
1XML被设计为传输和存储数据其焦点是数据的内容
2HTML是显示数据以及如何更好的显示数据
3.XML文档示例
?xml version1.0 encodingutf-8?bookstorebook categorycookingtitle langenEveryday Italian/title authorGiada De Laurentiis/author year2005/year price30.00/price/book book categorychildrentitle langenHarry Potter/title authorJ K. Rowling/author year2005/year price29.99/price/book book categorywebtitle langenXQuery Kick Start/title authorJames McGovern/author authorPer Bothner/author authorKurt Cagle/author authorJames Linn/author authorVaidyanathan Nagarajan/author year2003/year price49.99/price/bookbook categoryweb coverpaperbacktitle langenLearning XML/title authorErik T. Ray/author year2003/year price39.95/price/book/bookstore
注意这上面的标签都是自定义的
二、XML的节点关系
1.父parent
每个元素及属性都有一个父
下面这个XML例子中book元是title,author,year,price元素的父 ?xml version1.0 encodingutf-8? book titleHarry Potter/title authorJ K. Rowling/author year2005/year price29.99/price /book 2.子children
元素节点可能有零个一个或者多个子
在下面的例子中 title,author,year,price都是book元素的子 ?xml version1.0 encodingutf-8? book titleHarry Potter/title authorJ K. Rowling/author year2005/year price29.99/price /book 3.同胞sibling
拥有相同的父的节点
在下面例子中 title,author,year,price元素都是同胞 ?xml version1.0 encodingutf-8? book titleHarry Potter/title authorJ K. Rowling/author year2005/year price29.99/price /book 4.先辈ancestor
某节点的父父的父等等
下面例子中title元素的先辈是book和bookstore ?xml version1.0 encodingutf-8? bookstore book titleHarry Potter/title authorJ K. Rowling/author year2005/year price29.99/price /book /bookstore 5.后代
某节点的子子的子等等
下面例子中bookstore后代是book,title,author,year,price元素 ?xml version1.0 encodingutf-8? bookstore book titleHarry Potter/title authorJ K. Rowling/author year2005/year price29.99/price /book /bookstore 第二章 xpath
XPath原理先将HTML文档转为XML文档再用XPath查找HTML节点或元素
一、xpath简介
xpth解析
1本地文件 etree.parse
2服务器响应的数据 response.read().decode(utf-8) etree.HTML()
示例1
1.本地文件解析
解析_xpath的基本使用.html
!DOCTYPE html
html langen
headmeta charsetUTF-8/titleTitle/title
/head
bodyulli idl1 classc1北京/lili idl2上海/lili classc3深圳/lili idc3武汉/lili idc4广州/li/ululli大连/lili锦州/lili沈阳/li/ul
/body
/html #解析解析——xpath的基本使用.html‘本地文件
treeetree.parse(解析_xpath的基本使用.html)
print(tree)#lxml.etree._ElementTree object at 0x000001FC65813F88 二、节点选取
1.选取节点
XPath使用路径表达式来选取XML文档中的节点或者节点集这些路径表达式和我们在常规的电脑文件系统里看到的表达式非常相似。
最常用的路径表达式
表达式描述nodename选取此节点的所有子节点。/从根节点选取。//从匹配选择的当前节点选择文档中的节点而不考虑它们的位置。.选取当前节点。..选取当前节点的父节点。选取属性。
例如
bookstore选取 bookstore 元素的所有子节点。 bookstore/book选取属于 bookstore 的子元素的所有 book 元素。 //book选取所有 book 子元素而不管它们在文档中的位置。 bookstore//book选择属于 bookstore 元素的后代的所有 book 元素而不管它们位于 bookstore 之下的什么位置。 //lang选取名为 lang 的所有属性。
2.选取未知节点
通配符描述*匹配任何元素节点。*匹配任何属性节点。node()匹配任何类型的节点。
比如
路径表达式结果/bookstore/*选取 bookstore 元素的所有子元素。//*选取文档中的所有元素。html/node()/meta/*选择html下面任意节点下的meta节点的所有属性//title[*]选取所有带有属性的 title 元素。 3.选取若干路径
通过在路径表达式中使用“|”运算符您可以选取若干个路径。
比如
路径表达式结果//book/title | //book/price选取 book 元素的所有 title 和 price 元素。//title | //price选取文档中的所有 title 和 price 元素。/bookstore/book/title | //price选取属于 bookstore 元素的 book 元素的所有 title 元素以及文档中所有的 price 元素。
三、路径查询
tree.xpath(xpath路径) 1 //:查找所有的子孙节点不考虑层级关系 2 /:找直接子节点
1.查找ul下面的li li_listtree.xpath(//body/ul/li)
# #判断列表的长度
print(li_list)
print(len(li_list)) 2.谓词查询
如
//div[id]
//div[id属性值] #查找所有有id属性的li标签
#text()获取标签中的内容
li_listtree.xpath(//ul/li[id])
li_list1tree.xpath(//ul/li[id]/text())#[北京, 上海]
#查找idl1的li标签 注意引号问题
li_list2tree.xpath(//ul/li[idl1]/text())
print(li_list)
print(li_list1)
print(li_list2) 3.属性查询
如//class #查找id为l1的li标签的class的属性值
li_listtree.xpath(//ul/li[idl1]/class)#c1
print(li_list) 4.模糊查询
如
//div[contains(id,he)]
//div[starts-with(id,he)] #查找id中包含l的li标签
li_listtree.xpath(//ul/li[contains(id,l)]/text())#[北京, 上海]
print(li_list) 5.内容查询
如//div/h1/text() #查询id的值以c开头的标签
li_listtree.xpath(//ul/li[starts-with(id,c)]/text())#[武汉, 广州]
print(li_list) 6.逻辑运算
如
//div[idhead and classs_down]
//title|//price #查询id为l1和class为c1的标签
li_listtree.xpath(//ul/li[idl1 and classc1]/text())#[北京]
li_list1tree.xpath(//ul/li[idl1]/text() | //ul/li[idl2]/text())#[北京, 上海]
print(li_list)
print(len(li_list))
print(li_list1)
print(len(li_list1)) 四、代码示例
html
divulli classitem-0a hreflink1.htmlfirst item/a/lili classitem-1a hreflink2.htmlsecond item/a/lili classitem-inactivea hreflink3.htmlthird item/a/lili classitem-1a hreflink4.htmlfourth item/a/lili classitem-0a hreflink5.htmlfifth item/a/div
# 1使用lxml的etree类
from lxml import etree#,2利用etree.HTML()构造一个xpath解析对象转为xml文档
xml_docetree.HTML(html)
print(xml_doc)
print(-----*10)# etree.tostring()输出转换后的html代码
html_doc etree.tostring(xml_doc)
print(html_doc) #自动补全了body,html标签
print(type(html_doc)) # bytes类型
# print(-----*10)
print(html_doc.decode()) # 利用decode()方法将其转成str类型
print(type(html_doc.decode())) 注意
1只要涉及到条件加 []
2只要获取属性值加
3通过text()取内容