{"id":46,"date":"2025-04-07T19:29:40","date_gmt":"2025-04-07T11:29:40","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/?p=46"},"modified":"2025-04-07T19:29:40","modified_gmt":"2025-04-07T11:29:40","slug":"%e4%bd%bf%e7%94%a8-symfony-%e7%9a%84-cssselector-%e7%bb%84%e4%bb%b6%e5%b0%86-css-%e9%80%89%e6%8b%a9%e5%99%a8%e8%bd%ac%e6%8d%a2%e4%b8%ba-xpath-%e8%a1%a8%e8%be%be%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/www.zhaozhao123.cn\/php\/post\/46.html","title":{"rendered":"\u4f7f\u7528 Symfony \u7684 CssSelector \u7ec4\u4ef6\u5c06 CSS \u9009\u62e9\u5668\u8f6c\u6362\u4e3a XPath \u8868\u8fbe\u5f0f"},"content":{"rendered":"\n<p>\u4e3a\u4e86\u5b9e\u73b0\u7c7b\u4f3c jQuery \u7684 CSS \u9009\u62e9\u5668\u8bed\u6cd5\uff08\u5982 <code>p<\/code>\u3001<code>.container .para1<\/code>\uff09\uff0c\u6211\u4eec\u9700\u8981\u5f15\u5165\u4e00\u4e2a\u5de5\u5177\u5c06 CSS \u9009\u62e9\u5668\u8f6c\u6362\u4e3a XPath \u8868\u8fbe\u5f0f\u3002PHP \u4e2d\u53ef\u4ee5\u4f7f\u7528 Symfony \u7684 <code>CssSelector<\/code> \u7ec4\u4ef6\u6765\u5b8c\u6210\u8fd9\u4e00\u4efb\u52a1\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u91cd\u5199\u540e\u7684 <code>Query<\/code> \u7c7b\uff0c\u652f\u6301 CSS \u9009\u62e9\u5668\u98ce\u683c\u7684\u67e5\u8be2\uff1a<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u5f15\u5165 Symfony CssSelector<\/h3>\n\n\n\n<p>\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86 Symfony \u7684 <code>CssSelector<\/code> \u7ec4\u4ef6\u3002\u53ef\u4ee5\u901a\u8fc7 Composer \u5b89\u88c5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>composer require symfony\/css-selector<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u4fee\u6539 <code>Query<\/code> \u7c7b\u4ee5\u652f\u6301 CSS \u9009\u62e9\u5668<\/h3>\n\n\n\n<p>\u4e0b\u9762\u662f\u4fee\u6539\u540e\u7684\u5b8c\u6574\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nrequire_once 'vendor\/autoload.php'; \/\/ \u786e\u4fdd\u52a0\u8f7d Composer \u81ea\u52a8\u52a0\u8f7d\u6587\u4ef6\n\nuse SymfonyComponentCssSelectorCssSelectorConverter;\n\nclass Query {\n    private $dom;\n    private $xpath;\n    private $nodes = &#91;];\n\n    public function __construct($html = null) {\n        $this-&gt;dom = new DOMDocument();\n        libxml_use_internal_errors(true); \/\/ \u9632\u6b62 HTML \u89e3\u6790\u9519\u8bef\u629b\u51fa\u5f02\u5e38\n        if ($html) {\n            $this-&gt;dom-&gt;loadHTML($html);\n        }\n        $this-&gt;xpath = new DOMXPath($this-&gt;dom);\n        libxml_clear_errors();\n    }\n\n    \/\/ \u67e5\u627e\u8282\u70b9\uff0c\u652f\u6301 CSS \u9009\u62e9\u5668\n    public function find($selector) {\n        $converter = new CssSelectorConverter();\n        $xpathExpression = $converter-&gt;toXPath($selector);\n        $this-&gt;nodes = $this-&gt;xpath-&gt;query($xpathExpression);\n        return $this; \/\/ \u652f\u6301\u94fe\u5f0f\u8c03\u7528\n    }\n\n    \/\/ \u83b7\u53d6\u6240\u6709\u5339\u914d\u8282\u70b9\u7684\u6587\u672c\u5185\u5bb9\n    public function text() {\n        $texts = &#91;];\n        foreach ($this-&gt;nodes as $node) {\n            $texts&#91;] = $node-&gt;nodeValue;\n        }\n        return $texts;\n    }\n\n    \/\/ \u83b7\u53d6\u7b2c\u4e00\u4e2a\u5339\u914d\u8282\u70b9\u7684\u6587\u672c\u5185\u5bb9\n    public function firstText() {\n        if ($this-&gt;nodes-&gt;length &gt; 0) {\n            return $this-&gt;nodes-&gt;item(0)-&gt;nodeValue;\n        }\n        return null;\n    }\n\n    \/\/ \u83b7\u53d6\u6216\u8bbe\u7f6e HTML \u5185\u5bb9\n    public function html($newHtml = null) {\n        if ($newHtml !== null) {\n            foreach ($this-&gt;nodes as $node) {\n                \/\/ \u6e05\u7a7a\u8282\u70b9\u73b0\u6709\u5185\u5bb9\n                while ($node-&gt;firstChild) {\n                    $node-&gt;removeChild($node-&gt;firstChild);\n                }\n                \/\/ \u6dfb\u52a0\u65b0\u7684 HTML \u5185\u5bb9\n                $fragment = $this-&gt;dom-&gt;createDocumentFragment();\n                $fragment-&gt;appendXML($newHtml);\n                $node-&gt;appendChild($fragment);\n            }\n            return $this;\n        }\n        $htmls = &#91;];\n        foreach ($this-&gt;nodes as $node) {\n            $htmls&#91;] = $this-&gt;dom-&gt;saveHTML($node);\n        }\n        return $htmls;\n    }\n\n    \/\/ \u83b7\u53d6\u6216\u8bbe\u7f6e\u5c5e\u6027\u503c\n    public function attr($attribute, $value = null) {\n        if ($value !== null) {\n            foreach ($this-&gt;nodes as $node) {\n                $node-&gt;setAttribute($attribute, $value);\n            }\n            return $this;\n        }\n        $attrs = &#91;];\n        foreach ($this-&gt;nodes as $node) {\n            if ($node-&gt;hasAttribute($attribute)) {\n                $attrs&#91;] = $node-&gt;getAttribute($attribute);\n            }\n        }\n        return $attrs;\n    }\n\n    \/\/ \u5411\u5339\u914d\u8282\u70b9\u8ffd\u52a0\u5185\u5bb9\n    public function append($html) {\n        foreach ($this-&gt;nodes as $node) {\n            $fragment = $this-&gt;dom-&gt;createDocumentFragment();\n            $fragment-&gt;appendXML($html);\n            $node-&gt;appendChild($fragment);\n        }\n        return $this;\n    }\n\n    \/\/ \u5220\u9664\u5339\u914d\u7684\u8282\u70b9\n    public function remove() {\n        foreach ($this-&gt;nodes as $node) {\n            if ($node-&gt;parentNode) {\n                $node-&gt;parentNode-&gt;removeChild($node);\n            }\n        }\n        return $this;\n    }\n\n    \/\/ \u8f93\u51fa\u5b8c\u6574\u7684 HTML\n    public function saveHTML() {\n        return $this-&gt;dom-&gt;saveHTML();\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u4f7f\u7528\u793a\u4f8b<\/h3>\n\n\n\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4ee5\u4e0b HTML \u5185\u5bb9\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$html = &lt;&lt;&lt;HTML\n&lt;html&gt;\n&lt;body&gt;\n    &lt;div class=\"container\"&gt;\n        &lt;p id=\"para1\" class=\"text\"&gt;Hello, World!&lt;\/p&gt;\n        &lt;p id=\"para2\" class=\"text\"&gt;This is a test.&lt;\/p&gt;\n        &lt;a href=\"https:<a href=\"https:\/\/www.zhaozhao123.cn\/skin\/go?url=aHR0cDovL2V4YW1wbGUuY29t\" rel=\"noopener noreferrer nofollow\">\/\/example.com<\/a>\" title=\"Example Link\"&gt;Click Me&lt;\/a&gt;\n    &lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\nHTML;\n\n$query = new Query($html);\n\n\/\/ \u793a\u4f8b 1: \u67e5\u627e\u6240\u6709 p \u6807\u7b7e\u5e76\u83b7\u53d6\u5b83\u4eec\u7684\u6587\u672c\u5185\u5bb9\n$texts = $query-&gt;find('p')-&gt;text();\necho \"All paragraph texts:n\";\nprint_r($texts);\n\n\/\/ \u793a\u4f8b 2: \u67e5\u627e\u7b2c\u4e00\u4e2a p \u6807\u7b7e\u7684\u6587\u672c\u5185\u5bb9\n$firstText = $query-&gt;find('p')-&gt;firstText();\necho \"First paragraph text: $firstTextn\";\n\n\/\/ \u793a\u4f8b 3: \u901a\u8fc7\u7c7b\u540d\u67e5\u627e\u6bb5\u843d\n$textsByClass = $query-&gt;find('.container .text')-&gt;text();\necho \"Texts by class selector:n\";\nprint_r($textsByClass);\n\n\/\/ \u793a\u4f8b 4: \u901a\u8fc7 ID \u67e5\u627e\u6bb5\u843d\n$textById = $query-&gt;find('#para1')-&gt;text();\necho \"Text by ID selector:n\";\nprint_r($textById);\n\n\/\/ \u793a\u4f8b 5: \u4fee\u6539\u7b2c\u4e00\u4e2a p \u6807\u7b7e\u7684\u5185\u5bb9\n$query-&gt;find('p#para1')-&gt;html('&lt;strong&gt;New Content&lt;\/strong&gt;');\necho \"Modified HTML:n\";\necho $query-&gt;saveHTML();\n\n\/\/ \u793a\u4f8b 6: \u83b7\u53d6\u6240\u6709\u94fe\u63a5\u7684 href \u5c5e\u6027\n$hrefs = $query-&gt;find('a')-&gt;attr('href');\necho \"All href attributes:n\";\nprint_r($hrefs);\n\n\/\/ \u793a\u4f8b 7: \u5220\u9664\u7b2c\u4e8c\u4e2a p \u6807\u7b7e\n$query-&gt;find('p#para2')-&gt;remove();\necho \"HTML after removing second paragraph:n\";\necho $query-&gt;saveHTML();<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u7ed3\u679c\u89e3\u91ca<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b 1: \u67e5\u627e\u6240\u6709 <code>p<\/code> \u6807\u7b7e<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>All paragraph texts:\nArray\n(\n    &#91;0] =&gt; Hello, World!\n    &#91;1] =&gt; This is a test.\n)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b 2: \u67e5\u627e\u7b2c\u4e00\u4e2a <code>p<\/code> \u6807\u7b7e<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>First paragraph text: Hello, World!<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b 3: \u901a\u8fc7\u7c7b\u540d <code>.container .text<\/code> \u67e5\u627e<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>Texts by class selector:\nArray\n(\n    &#91;0] =&gt; Hello, World!\n    &#91;1] =&gt; This is a test.\n)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b 4: \u901a\u8fc7 ID <code>#para1<\/code> \u67e5\u627e<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>Text by ID selector:\nArray\n(\n    &#91;0] =&gt; Hello, World!\n)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b 5: \u4fee\u6539\u7b2c\u4e00\u4e2a\u6bb5\u843d\u5185\u5bb9<\/h4>\n\n\n\n<p>\u4fee\u6539\u540e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;p id=\"para1\" class=\"text\"&gt;&lt;strong&gt;New Content&lt;\/strong&gt;&lt;\/p&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b 6: \u83b7\u53d6\u6240\u6709\u94fe\u63a5\u7684 <code>href<\/code> \u5c5e\u6027<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>All href attributes:\nArray\n(\n    &#91;0] =&gt; <a href=\"https:\/\/www.zhaozhao123.cn\/skin\/go?url=aHR0cHM6Ly9leGFtcGxlLmNvbQ==\" rel=\"noopener noreferrer nofollow\">https:\/\/example.com<\/a>\n)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b 7: \u5220\u9664\u7b2c\u4e8c\u4e2a\u6bb5\u843d<\/h4>\n\n\n\n<p>\u5220\u9664\u540e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div class=\"container\"&gt;\n    &lt;p id=\"para1\" class=\"text\"&gt;&lt;strong&gt;New Content&lt;\/strong&gt;&lt;\/p&gt;\n    &lt;a href=\"https:<a href=\"https:\/\/www.zhaozhao123.cn\/skin\/go?url=aHR0cDovL2V4YW1wbGUuY29t\" rel=\"noopener noreferrer nofollow\">\/\/example.com<\/a>\" title=\"Example Link\"&gt;Click Me&lt;\/a&gt;\n&lt;\/div&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u603b\u7ed3<\/h3>\n\n\n\n<p>\u901a\u8fc7\u5f15\u5165 Symfony \u7684 <code>CssSelector<\/code> \u7ec4\u4ef6\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06 CSS \u9009\u62e9\u5668\u8f6c\u6362\u4e3a XPath \u8868\u8fbe\u5f0f\uff0c\u4ece\u800c\u5b9e\u73b0\u7c7b\u4f3c jQuery \u7684\u67e5\u8be2\u65b9\u5f0f\u3002\u8fd9\u4e2a\u6539\u8fdb\u8ba9 <code>Query<\/code> \u7c7b\u66f4\u52a0\u7075\u6d3b\u548c\u6613\u7528\uff0c\u652f\u6301\u4ee5\u4e0b\u529f\u80fd\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>CSS \u9009\u62e9\u5668<\/strong>\uff1a\u5982 <code>p<\/code>\u3001<code>.class<\/code>\u3001<code>#id<\/code>\u3001<code>.container .text<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u6587\u672c\u63d0\u53d6<\/strong>\uff1a<code>text()<\/code> \u548c <code>firstText()<\/code>\u3002<\/li>\n\n\n\n<li><strong>HTML \u64cd\u4f5c<\/strong>\uff1a<code>html()<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u5c5e\u6027\u64cd\u4f5c<\/strong>\uff1a<code>attr()<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u8282\u70b9\u6dfb\u52a0\/\u5220\u9664<\/strong>\uff1a<code>append()<\/code> \u548c <code>remove()<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u79cd\u5c01\u88c5\u65b9\u5f0f\u6781\u5927\u5730\u7b80\u5316\u4e86 PHP \u4e2d\u7684 DOM \u64cd\u4f5c\uff01<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3a\u4e86\u5b9e\u73b0\u7c7b\u4f3c jQuery \u7684 CSS \u9009\u62e9\u5668\u8bed\u6cd5\uff08\u5982 p\u3001.container .para1\uff09\uff0c\u6211\u4eec\u9700\u8981\u5f15\u5165\u4e00\u4e2a\u5de5\u5177\u5c06 CSS \u9009\u62e9\u5668\u8f6c\u6362\u4e3a XPath \u8868\u8fbe\u5f0f\u3002PHP \u4e2d\u53ef\u4ee5\u4f7f\u7528 Symfony \u7684 CssSelector \u7ec4\u4ef6\u6765\u5b8c\u6210\u8fd9\u4e00\u4efb\u52a1\u3002 \u4ee5\u4e0b\u662f\u91cd\u5199\u540e\u7684 Query \u7c7b\uff0c\u652f\u6301 CSS \u9009\u62e9\u5668\u98ce\u683c\u7684\u67e5\u8be2\uff1a 1&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"tuisongtax":[],"class_list":["post-46","post","type-post","status-publish","format-standard","hentry","category-jswz"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/posts\/46","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/comments?post=46"}],"version-history":[{"count":0,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/posts\/46\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/media?parent=46"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/categories?post=46"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=46"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=46"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}