{"id":949777,"date":"2024-12-27T00:16:21","date_gmt":"2024-12-26T16:16:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/949777.html"},"modified":"2024-12-27T00:16:23","modified_gmt":"2024-12-26T16:16:23","slug":"python%e5%a6%82%e4%bd%95%e9%81%8d%e5%8e%86dom","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/949777.html","title":{"rendered":"python\u5982\u4f55\u904d\u5386dom"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084632\/112c166d-f3aa-4437-9db8-063bbd82db5a.webp\" alt=\"python\u5982\u4f55\u904d\u5386dom\" \/><\/p>\n<p><p> \u4e00\u3001\u4f7f\u7528Beautiful Soup\u904d\u5386DOM\u3001\u89e3\u6790\u548c\u64cd\u4f5cHTML\u6587\u6863<\/p>\n<\/p>\n<p><p><strong>Python\u904d\u5386DOM\u53ef\u4ee5\u4f7f\u7528Beautiful Soup\u5e93\u3001lxml\u5e93\u3001Selenium\u5e93\u7b49\u3002<\/strong> Beautiful Soup\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5e93\uff0c\u7528\u4e8e\u89e3\u6790\u548c\u64cd\u4f5cHTML\u6587\u6863\u3002\u5b83\u63d0\u4f9b\u4e86\u7b80\u5355\u7684API\u6765\u904d\u5386DOM\u6811\uff0c\u63d0\u53d6\u8282\u70b9\u4fe1\u606f\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5Beautiful Soup\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u5b89\u88c5\uff1a<code>pip install beautifulsoup4<\/code>\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u89e3\u6790HTML\u6587\u6863\uff0c\u5e76\u904d\u5386DOM\u6811\u3002<\/p>\n<\/p>\n<p><p>Beautiful Soup\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\u6765\u89e3\u6790HTML\u6587\u6863\uff0c\u7136\u540e\u4f7f\u7528\u8be5\u5bf9\u8c61\u63d0\u4f9b\u7684\u65b9\u6cd5\u53ef\u4ee5\u8f7b\u677e\u904d\u5386DOM\u6811\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>find_all()\u65b9\u6cd5<\/strong>\uff1a\u7528\u4e8e\u67e5\u627e\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684\u6807\u7b7e\uff0c\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\u3002\u6bd4\u5982\uff0c\u627e\u5230\u6240\u6709\u7684<code>&lt;a&gt;<\/code>\u6807\u7b7e\uff1a<code>soup.find_all(&#39;a&#39;)<\/code>\u3002\u8fd9\u5728\u9700\u8981\u83b7\u53d6\u9875\u9762\u4e2d\u6240\u6709\u94fe\u63a5\u65f6\u7279\u522b\u6709\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>find()\u65b9\u6cd5<\/strong>\uff1a\u7528\u4e8e\u67e5\u627e\u7b2c\u4e00\u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u6807\u7b7e\uff0c\u8fd4\u56de\u4e00\u4e2a\u5355\u4e00\u5bf9\u8c61\u3002\u5982\u679c\u53ea\u60f3\u627e\u5230\u7b2c\u4e00\u4e2a<code>&lt;div&gt;<\/code>\u6807\u7b7e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>soup.find(&#39;div&#39;)<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>children\u5c5e\u6027<\/strong>\uff1a\u7528\u4e8e\u904d\u5386\u67d0\u4e2a\u8282\u70b9\u7684\u76f4\u63a5\u5b50\u8282\u70b9\u3002\u4f8b\u5982\uff0c\u8981\u904d\u5386\u67d0\u4e2a\u6807\u7b7e\u4e0b\u7684\u6240\u6709\u76f4\u63a5\u5b50\u8282\u70b9\uff0c\u53ef\u4ee5\u4f7f\u7528<code>tag.children<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528lxml\u5e93\u89e3\u6790\u548c\u904d\u5386DOM<\/p>\n<\/p>\n<p><p>lxml\u662f\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684\u5e93\uff0c\u7528\u4e8e\u89e3\u6790\u548c\u64cd\u4f5cHTML\/XML\u6587\u6863\u3002\u5b83\u6bd4Beautiful Soup\u66f4\u5feb\uff0c\u652f\u6301XPath\u67e5\u8be2\u3002\u5b89\u88c5lxml\u5e93\u53ef\u4ee5\u901a\u8fc7<code>pip install lxml<\/code>\u547d\u4ee4\u3002lxml\u5e93\u7684\u6838\u5fc3\u662fElementTree API\uff0c\u5b83\u4f7f\u5f97\u89e3\u6790\u548c\u904d\u5386DOM\u975e\u5e38\u9ad8\u6548\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528etree\u6a21\u5757<\/strong>\uff1alxml\u7684etree\u6a21\u5757\u63d0\u4f9b\u4e86\u89e3\u6790HTML\/XML\u7684\u529f\u80fd\u3002\u901a\u8fc7\u4f7f\u7528<code>etree.HTML()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u89e3\u6790HTML\u6587\u672c\u5e76\u751f\u6210\u4e00\u4e2aElement\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>XPath\u67e5\u8be2<\/strong>\uff1alxml\u652f\u6301XPath\u67e5\u8be2\uff0c\u8fd9\u4f7f\u5f97\u641c\u7d22DOM\u5143\u7d20\u66f4\u52a0\u7075\u6d3b\u548c\u5f3a\u5927\u3002XPath\u662f\u4e00\u79cd\u7528\u4e8e\u5728XML\u6587\u6863\u4e2d\u67e5\u627e\u4fe1\u606f\u7684\u8bed\u8a00\u3002\u4f8b\u5982\uff0c\u67e5\u627e\u6240\u6709<code>&lt;a&gt;<\/code>\u6807\u7b7e\uff1a<code>tree.xpath(&#39;\/\/a&#39;)<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u904d\u5386\u5b50\u8282\u70b9<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7\u8fed\u4ee3Element\u5bf9\u8c61\u6765\u904d\u5386\u5176\u5b50\u8282\u70b9\u3002\u4f8b\u5982\uff1a<code>for element in root:<\/code>\u53ef\u4ee5\u904d\u5386\u6839\u8282\u70b9\u7684\u6240\u6709\u76f4\u63a5\u5b50\u8282\u70b9\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528Selenium\u5e93\u8fdb\u884c\u52a8\u6001DOM\u904d\u5386<\/p>\n<\/p>\n<p><p>Selenium\u662f\u4e00\u4e2a\u7528\u4e8e\u81ea\u52a8\u5316Web\u6d4f\u89c8\u5668\u7684\u5de5\u5177\uff0c\u5e38\u7528\u4e8e\u6d4b\u8bd5Web\u5e94\u7528\u7a0b\u5e8f\u3002\u5b83\u652f\u6301\u52a8\u6001\u5185\u5bb9\u52a0\u8f7d\uff0c\u56e0\u6b64\u53ef\u4ee5\u5904\u7406JavaScript\u751f\u6210\u7684\u5185\u5bb9\u3002Selenium\u53ef\u4ee5\u4e0ePython\u7ed3\u5408\u4f7f\u7528\u6765\u904d\u5386DOM\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>WebDriver\u5bf9\u8c61<\/strong>\uff1aSelenium\u901a\u8fc7WebDriver\u5bf9\u8c61\u63a7\u5236\u6d4f\u89c8\u5668\u3002\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u6d4f\u89c8\u5668\u9a71\u52a8\u7a0b\u5e8f\uff0c\u5982ChromeDriver\u548cGeckoDriver\u3002\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5Selenium\u5e93\uff1a<code>pip install selenium<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9875\u9762\u5143\u7d20\u7684\u67e5\u627e<\/strong>\uff1aSelenium\u63d0\u4f9b\u4e86\u591a\u79cd\u67e5\u627e\u9875\u9762\u5143\u7d20\u7684\u65b9\u6cd5\uff0c\u5982<code>find_element_by_id()<\/code>\u3001<code>find_elements_by_tag_name()<\/code>\u7b49\u3002\u8fd9\u4e9b\u65b9\u6cd5\u8fd4\u56deWebElement\u5bf9\u8c61\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u52a8\u6001\u5185\u5bb9\u5904\u7406<\/strong>\uff1a\u5bf9\u4e8e\u9700\u8981\u7b49\u5f85\u52a8\u6001\u5185\u5bb9\u52a0\u8f7d\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528Selenium\u7684<code>WebDriverW<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t<\/code>\u7c7b\u7b49\u5f85\u67d0\u4e2a\u6761\u4ef6\u6210\u7acb\uff0c\u5982\u67d0\u4e2a\u5143\u7d20\u53ef\u89c1\u540e\u518d\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u548c\u81ea\u5b9a\u4e49\u89e3\u6790<\/p>\n<\/p>\n<p><p>\u5c3d\u7ba1Beautiful Soup\u548clxml\u662f\u5904\u7406HTML\u7684\u6700\u4f73\u9009\u62e9\uff0c\u6709\u65f6\u5019\u81ea\u5b9a\u4e49\u89e3\u6790\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u4e5f\u662f\u89e3\u51b3\u95ee\u9898\u7684\u9014\u5f84\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\uff1aPython\u7684<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u652f\u6301\uff0c\u7528\u4e8e\u5339\u914d\u548c\u63d0\u53d6HTML\u4e2d\u7684\u7279\u5b9a\u6a21\u5f0f\u3002\u4f46\u8fd9\u901a\u5e38\u4e0d\u662f\u63a8\u8350\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3aHTML\u7684\u7ed3\u6784\u53ef\u80fd\u590d\u6742\u4e14\u4e0d\u89c4\u5219\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u81ea\u5b9a\u4e49\u89e3\u6790<\/strong>\uff1a\u5728\u67d0\u4e9b\u7279\u5b9a\u573a\u666f\u4e0b\uff0c\u81ea\u5b9a\u4e49\u89e3\u6790HTML\u53ef\u80fd\u66f4\u6709\u6548\u3002\u8fd9\u9700\u8981\u5bf9HTML\u7ed3\u6784\u6709\u6df1\u523b\u7406\u89e3\uff0c\u5e76\u7f16\u5199\u4ee3\u7801\u6765\u624b\u52a8\u904d\u5386\u548c\u63d0\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u603b\u7ed3\u4e0e\u6700\u4f73\u5b9e\u8df5<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406HTML\u6587\u6863\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u81f3\u5173\u91cd\u8981\u3002<strong>Beautiful Soup\u9002\u5408\u5feb\u901f\u5f00\u53d1\u548c\u7b80\u5355\u7684HTML\u89e3\u6790\uff0clxml\u5219\u63d0\u4f9b\u66f4\u9ad8\u6548\u7684\u89e3\u6790\u548cXPath\u652f\u6301\uff0cSelenium\u53ef\u4ee5\u5904\u7406\u52a8\u6001\u5185\u5bb9\u548cJavaScript\u751f\u6210\u7684\u5185\u5bb9\u3002<\/strong>\u5bf9\u4e8e\u590d\u6742\u7684\u89e3\u6790\u4efb\u52a1\uff0c\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u591a\u79cd\u5de5\u5177\u548c\u6280\u672f\u3002\u786e\u4fdd\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u4e0d\u89c4\u5219\u6216\u52a8\u6001\u751f\u6210\u7684HTML\u65f6\uff0c\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u548c\u65e5\u5fd7\u8bb0\u5f55\u4ee5\u4fbf\u8c03\u8bd5\u548c\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u904d\u5386HTML\u6587\u6863\u4e2d\u7684DOM\u7ed3\u6784\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5e93\u5982BeautifulSoup\u548clxml\u6765\u904d\u5386DOM\u7ed3\u6784\u3002BeautifulSoup\u63d0\u4f9b\u4e86\u7b80\u6d01\u7684API\uff0c\u9002\u5408\u5feb\u901f\u89e3\u6790\u548c\u904d\u5386HTML\u6587\u6863\u3002\u901a\u8fc7\u4f7f\u7528find_all\u3001select\u7b49\u65b9\u6cd5\uff0c\u7528\u6237\u53ef\u4ee5\u8f7b\u677e\u627e\u5230\u7279\u5b9a\u7684\u6807\u7b7e\u3001\u7c7b\u6216\u5c5e\u6027\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u904d\u5386DOM\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u6027\u80fd\u95ee\u9898\uff1f<\/strong><br \/>\u904d\u5386DOM\u65f6\uff0c\u5c24\u5176\u662f\u5904\u7406\u5927\u578bHTML\u6587\u6863\uff0c\u6027\u80fd\u53ef\u80fd\u6210\u4e3a\u4e00\u4e2a\u95ee\u9898\u3002\u9009\u62e9\u5408\u9002\u7684\u89e3\u6790\u5e93\u975e\u5e38\u91cd\u8981\u3002\u4f8b\u5982\uff0clxml\u5728\u5904\u7406\u5927\u6587\u4ef6\u65f6\u8868\u73b0\u5f97\u66f4\u5feb\u3002\u5c3d\u91cf\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u904d\u5386\u64cd\u4f5c\uff0c\u4f7f\u7528CSS\u9009\u62e9\u5668\u6216XPath\u8868\u8fbe\u5f0f\u53ef\u4ee5\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<p><strong>Python\u662f\u5426\u652f\u6301\u52a8\u6001DOM\u904d\u5386\uff1f<\/strong><br \/>Python\u53ef\u4ee5\u4f7f\u7528Selenium\u7b49\u5de5\u5177\u5b9e\u73b0\u52a8\u6001DOM\u904d\u5386\u3002Selenium\u80fd\u591f\u6a21\u62df\u7528\u6237\u4e0e\u7f51\u9875\u7684\u4ea4\u4e92\uff0c\u9002\u5408\u5904\u7406JavaScript\u751f\u6210\u7684\u5185\u5bb9\u3002\u901a\u8fc7\u7b49\u5f85\u5143\u7d20\u52a0\u8f7d\u548c\u52a8\u6001\u83b7\u53d6\u5185\u5bb9\uff0c\u7528\u6237\u53ef\u4ee5\u6709\u6548\u5730\u904d\u5386\u548c\u63d0\u53d6\u6240\u9700\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4e00\u3001\u4f7f\u7528Beautiful Soup\u904d\u5386DOM\u3001\u89e3\u6790\u548c\u64cd\u4f5cHTML\u6587\u6863 Python\u904d\u5386DOM\u53ef\u4ee5\u4f7f\u7528Beau [&hellip;]","protected":false},"author":3,"featured_media":949779,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/949777"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=949777"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/949777\/revisions"}],"predecessor-version":[{"id":949780,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/949777\/revisions\/949780"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/949779"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=949777"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=949777"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=949777"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}