XPath与lxml解析库
test.xml?xml version1.0 encodingutf-8? bookstore book namehalibote title langenHarry Potter/title authorJ K. Rowling/author year2005/year price29.99/price abc book lang中文neibu/book /abc /book book namehongloumeng 红楼梦 /book /bookstorehello.html!DOCTYPE html html langen head meta charsetUTF-8 titleTitle/title /head body !-- hello.html -- div ul li classitem-0meiguoa hreflink1.htmlfirst item/a/li li classitem-1a hreflink2.htmlsecond item/a/li li classitem-inactivea hreflink3.htmlspan classboldthird item/span/a/li li classitem-1a hreflink4.htmlfourth item/a/li li classitem-0a hreflink5.htmlfifth item/a/li /ul /div /body /html选取节点from lxml import etree tree etree.parse(test.xml) list_node tree.xpath(book/name) print(list_node[0]) list_node tree.xpath(/bookstore) print(list_node[0]) list_node tree.xpath(book/title) print(list_node[0].text) list_node tree.xpath(book//book) print(list_node) list_node tree.xpath(//lang) print(list_node)谓语指路径表达式的附加条件from lxml import etree tree etree.parse(test.xml) list_node tree.xpath(book[2]) print(list_node[0].text)选取未知节点from lxml import etree tree etree.parse(test.xml) list_node tree.xpath(/bookstore/*) print(list_node)选取若干路径from lxml import etree tree etree.parse(test.xml) list_node tree.xpath(//book/title | //book/price) print(list_node)通过轴限定from lxml import etree tree etree.parse(test.xml) list_node tree.xpath(descendant::book) print(list_node)操作XML节点from lxml import etree root etree.Element(root,a1) child etree.SubElement(root, child) root.set(b, 2) root.text yilang print(etree.tostring(root)) print(root.tag) print(root.text) # 从字符串中解析XML返回根节点 root etree.XML(root a x123aText b/ c/ b/ /a /root) # 从根节点查找返回匹配到的节点名称 print(root.find(a).tag) # 从根节点开始查找返回匹配到的第一个节点的名称 print(root.findall(.//a[x])[0].tag)在XML中搜索from lxml import etree tree etree.parse(hello.html,parseretree.HTMLParser()) list_node tree.xpath(//li) print(list_node[0].text)
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2467702.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!