<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>XML on Blogs (码码要洗手)</title><link>https://navydev.top/blog/tags/xml/</link><description>Recent content in XML on Blogs (码码要洗手)</description><generator>Hugo -- gohugo.io</generator><language>zh-CN</language><lastBuildDate>Wed, 31 Jul 2024 10:50:18 +0800</lastBuildDate><atom:link href="https://navydev.top/blog/tags/xml/index.xml" rel="self" type="application/rss+xml"/><item><title>XPath</title><link>https://navydev.top/blog/xpath/</link><pubDate>Wed, 31 Jul 2024 10:50:18 +0800</pubDate><guid>https://navydev.top/blog/xpath/</guid><description>&lt;img src="https://navydev.top/images/covers/xpath.webp" alt="Featured image of post XPath" /&gt;&lt;p&gt;XPath (XML Path Language) is a query language for selecting nodes from an XML document. It&amp;rsquo;s widely used in web scraping (e.g., Selenium, Scrapy) to locate elements in HTML.&lt;/p&gt;
&lt;p&gt;&lt;a class="link" href="https://www.bilibili.com/video/BV1Kd4y1R7Bt/" target="_blank" rel="noopener"
&gt;视频解说点这里&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="xpath-syntax-cheat-sheet"&gt;XPath Syntax Cheat Sheet
&lt;/h2&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style="text-align: left"&gt;Component&lt;/th&gt;
&lt;th style="text-align: left"&gt;Expression&lt;/th&gt;
&lt;th style="text-align: left"&gt;Example&lt;/th&gt;
&lt;th style="text-align: left"&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="text-align: left"&gt;&lt;strong&gt;Node Selection&lt;/strong&gt;&lt;/td&gt;
&lt;td style="text-align: left"&gt;&lt;code&gt;/&lt;/code&gt;&lt;/td&gt;
&lt;td style="text-align: left"&gt;&lt;code&gt;/html/body/div&lt;/code&gt;&lt;/td&gt;
&lt;td style="text-align: left"&gt;Selects from the root node.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align: left"&gt;&lt;/td&gt;
&lt;td style="text-align: left"&gt;&lt;code&gt;//&lt;/code&gt;&lt;/td&gt;
&lt;td style="text-align: left"&gt;&lt;code&gt;//div&lt;/code&gt;&lt;/td&gt;
&lt;td style="text-align: left"&gt;Selects nodes in the document from the current node that match the selection no matter where they are.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align: left"&gt;&lt;/td&gt;
&lt;td style="text-align: left"&gt;&lt;code&gt;.&lt;/code&gt;&lt;/td&gt;
&lt;td style="text-align: left"&gt;&lt;code&gt;./div&lt;/code&gt;&lt;/td&gt;
&lt;td style="text-align: left"&gt;Selects the current node.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align: left"&gt;&lt;/td&gt;
&lt;td style="text-align: left"&gt;&lt;code&gt;..&lt;/code&gt;&lt;/td&gt;
&lt;td style="text-align: left"&gt;&lt;code&gt;../div&lt;/code&gt;&lt;/td&gt;
&lt;td style="text-align: left"&gt;Selects the parent of the current node.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align: left"&gt;&lt;strong&gt;Attributes&lt;/strong&gt;&lt;/td&gt;
&lt;td style="text-align: left"&gt;&lt;code&gt;@&lt;/code&gt;&lt;/td&gt;
&lt;td style="text-align: left"&gt;&lt;code&gt;//@href&lt;/code&gt;&lt;/td&gt;
&lt;td style="text-align: left"&gt;Selects attributes.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align: left"&gt;&lt;strong&gt;Predicates&lt;/strong&gt;&lt;/td&gt;
&lt;td style="text-align: left"&gt;&lt;code&gt;[]&lt;/code&gt;&lt;/td&gt;
&lt;td style="text-align: left"&gt;&lt;code&gt;//div[@class='main']&lt;/code&gt;&lt;/td&gt;
&lt;td style="text-align: left"&gt;Filter expressions.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align: left"&gt;&lt;strong&gt;Wildcards&lt;/strong&gt;&lt;/td&gt;
&lt;td style="text-align: left"&gt;&lt;code&gt;*&lt;/code&gt;&lt;/td&gt;
&lt;td style="text-align: left"&gt;&lt;code&gt;//*&lt;/code&gt;&lt;/td&gt;
&lt;td style="text-align: left"&gt;Matches any element node.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h2 id="common-examples"&gt;Common Examples
&lt;/h2&gt;&lt;h3 id="selecting-elements"&gt;Selecting Elements
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;&lt;code&gt;//div&lt;/code&gt;: Select all &lt;code&gt;div&lt;/code&gt; elements.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;//div/p&lt;/code&gt;: Select all &lt;code&gt;p&lt;/code&gt; elements that are children of &lt;code&gt;div&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;//div//p&lt;/code&gt;: Select all &lt;code&gt;p&lt;/code&gt; elements that are descendants of &lt;code&gt;div&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="using-attributes"&gt;Using Attributes
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;&lt;code&gt;//div[@id='content']&lt;/code&gt;: Select &lt;code&gt;div&lt;/code&gt; with &lt;code&gt;id=&amp;quot;content&amp;quot;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;//a[@href='google.com']&lt;/code&gt;: Select link pointing to google.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;//input[@type='text']&lt;/code&gt;: Select text input fields.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="functions"&gt;Functions
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;&lt;code&gt;contains()&lt;/code&gt;: &lt;code&gt;//div[contains(@class, 'nav')]&lt;/code&gt; (Matches &lt;code&gt;class=&amp;quot;main-nav&amp;quot;&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;starts-with()&lt;/code&gt;: &lt;code&gt;//div[starts-with(@id, 'auth-')]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;text()&lt;/code&gt;: &lt;code&gt;//button[text()='Submit']&lt;/code&gt; (Exact match)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;last()&lt;/code&gt;: &lt;code&gt;//ul/li[last()]&lt;/code&gt; (Select the last item)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;position()&lt;/code&gt;: &lt;code&gt;//ul/li[position() &amp;lt; 3]&lt;/code&gt; (Select first two items)&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="axes"&gt;Axes
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;&lt;code&gt;//h1/following-sibling::p&lt;/code&gt;: Select &lt;code&gt;p&lt;/code&gt; elements after &lt;code&gt;h1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;//p/parent::div&lt;/code&gt;: Select the &lt;code&gt;div&lt;/code&gt; parent of &lt;code&gt;p&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="tools"&gt;Tools
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Chrome DevTools&lt;/strong&gt;: &lt;code&gt;Cmd+F&lt;/code&gt; in Elements tab supports XPath.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;XPath Helper&lt;/strong&gt;: Chrome extension for testing XPath.
| 操作符 | and, or, &amp;gt;=, &amp;lt;= &amp;hellip; |&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="如何使用-xpath"&gt;如何使用 XPath
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;Chrome Elements
&lt;img src="https://navydev.top/blog/images/xpath/elements.png"
loading="lazy"
alt="Elements"
&gt;&lt;/li&gt;
&lt;li&gt;Chrome Console
&lt;img src="https://navydev.top/blog/images/xpath/console.png"
loading="lazy"
alt="Console"
&gt;&lt;/li&gt;
&lt;li&gt;Scrapy shell
&lt;img src="https://navydev.top/blog/images/xpath/scrapy.png"
loading="lazy"
alt="Scrapy"
&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="xpath-案例"&gt;XPath 案例
&lt;/h2&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style="text-align: center"&gt;XPath&lt;/th&gt;
&lt;th style="text-align: center"&gt;Comment&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="text-align: center"&gt;//ul/li/a&lt;/td&gt;
&lt;td style="text-align: center"&gt;纯节点&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align: center"&gt;//button/text()[.=‘Submit’]&lt;/td&gt;
&lt;td style="text-align: center"&gt;限定符&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align: center"&gt;//a[@id=“abc”][@for=“xyz”]&lt;/td&gt;
&lt;td style="text-align: center"&gt;多限定符&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align: center"&gt;//a[@name or @href]&lt;/td&gt;
&lt;td style="text-align: center"&gt;操作符&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align: center"&gt;//a[starts-with(@href, “/”)]&lt;/td&gt;
&lt;td style="text-align: center"&gt;函数&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align: center"&gt;//ul[count(li) &amp;gt; 2 and li[ends-with(@class, “active”)]]&lt;/td&gt;
&lt;td style="text-align: center"&gt;复合条件&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align: center"&gt;//ul/li[last()]&lt;/td&gt;
&lt;td style="text-align: center"&gt;位置&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align: center"&gt;//h1/following-sibling::ul&lt;/td&gt;
&lt;td style="text-align: center"&gt;轴&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align: center"&gt;//div[count(./div)=2] | //tr[count(./td)=2]&lt;/td&gt;
&lt;td style="text-align: center"&gt;合集&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h2 id="xpath-要注意的-11-个点"&gt;XPath 要注意的 11 个点
&lt;/h2&gt;&lt;ol&gt;
&lt;li&gt;返回的是节点集合。&lt;/li&gt;
&lt;li&gt;text() 也是节点，可以限定。&lt;/li&gt;
&lt;li&gt;[] 可联结(Chainable: &lt;code&gt;[][]&lt;/code&gt;), 可嵌套(Nestable: &lt;code&gt;[[]]&lt;/code&gt;)。&lt;/li&gt;
&lt;li&gt;路径表达式可以通过 &amp;ldquo;|&amp;rdquo; 获取合集。&lt;/li&gt;
&lt;li&gt;source code 里 &lt;strong&gt;tbody&lt;/strong&gt; 标签可能不存在。&lt;/li&gt;
&lt;li&gt;善用函数 &lt;code&gt;substring-before(XPath, &amp;quot;split_str&amp;quot;)&lt;/code&gt;, &lt;code&gt;concat(XPath, XPath)&lt;/code&gt;。&lt;/li&gt;
&lt;li&gt;注意函数的作用域，往往只作用于节点集的第一个节点。&lt;/li&gt;
&lt;li&gt;当前节点它是一个引用，依然可以搜索整棵树。&lt;/li&gt;
&lt;li&gt;可结合 re 的功能。&lt;/li&gt;
&lt;li&gt;强烈推荐 Python package parsel，它还支持 JMESPath。&lt;/li&gt;
&lt;li&gt;如果是 XML 类型，需要声明 namespace 及替换没有 namespace 的 soap。&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id="参考"&gt;参考
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://devhints.io/xpath" target="_blank" rel="noopener"
&gt;XPath cheatsheet&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://www.zyte.com/blog/xpath-tips-from-the-web-scraping-trenches/" target="_blank" rel="noopener"
&gt;XPath tips&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://developer.mozilla.org/en-US/docs/Web/XPath/Functions" target="_blank" rel="noopener"
&gt;XPath functions&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item></channel></rss>