{"id":232057,"date":"2024-05-11T22:32:35","date_gmt":"2024-05-11T14:32:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/232057.html"},"modified":"2024-05-11T22:32:40","modified_gmt":"2024-05-11T14:32:40","slug":"%e7%88%ac%e8%99%abspan%e5%a4%96%e9%9d%a2%ef%bc%8cp%e9%87%8c%e9%9d%a2%e7%9a%84%e5%ad%97%e6%ae%b5%e6%80%8e%e4%b9%88%e7%88%ac%e5%8f%96","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/232057.html","title":{"rendered":"\u722c\u866bspan\u5916\u9762\uff0cp\u91cc\u9762\u7684\u5b57\u6bb5\u600e\u4e48\u722c\u53d6"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25154341\/fd9f0bda-0b33-42b8-96bc-0bf6c6316b07.webp\" alt=\"\u722c\u866bspan\u5916\u9762\uff0cp\u91cc\u9762\u7684\u5b57\u6bb5\u600e\u4e48\u722c\u53d6\" \/><\/p>\n<p><p>\u722c\u53d6\u7f51\u9875\u4e2d\u7279\u5b9a\u7684\u5b57\u6bb5\u901a\u5e38\u9700\u8981\u4f7f\u7528\u7f51\u7edc\u722c\u866b\uff0c\u5e76\u5bf9HTML\u5143\u7d20\u8fdb\u884c\u67e5\u8be2\u548c\u89e3\u6790\u3002\u5bf9\u4e8e\u4e00\u4e2a\u4f4d\u4e8e<code>span<\/code> \u6807\u7b7e\u5916\u9762\u3001<code>p<\/code> \u6807\u7b7e\u91cc\u9762\u7684\u6587\u672c\uff0c\u53ef\u4ee5\u901a\u8fc7HTML\u89e3\u6790\u5e93\u5982BeautifulSoup\u6216lxml\u7b49\u6765\u5b9e\u73b0\u3002<strong>\u9996\u5148\uff0c\u719f\u6089\u76ee\u6807\u7f51\u9875\u7684\u7ed3\u6784\u662f\u5173\u952e<\/strong>\u3001\u7136\u540e\u9009\u62e9\u5408\u9002\u7684\u9009\u62e9\u5668\u6765\u5b9a\u4f4d\u8fd9\u4e9b\u5143\u7d20\u3002\u8fd9\u91cc\uff0c\u53ef\u4ee5<strong>\u901a\u8fc7\u7236\u5b50\u6216\u5144\u5f1f\u9009\u62e9\u5668<\/strong>\u627e\u5230\u8fd9\u4e9b\u6587\u672c\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\u662f\u8be6\u7ec6\u63cf\u8ff0\u8fd9\u4e00\u6838\u5fc3\u89c2\u70b9\uff1a<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u68c0\u67e5HTML\u6587\u6863\uff0c\u627e\u5230<code>p<\/code> \u6807\u7b7e\u5185\u5305\u542b\u7684<code>span<\/code> \u6807\u7b7e\uff0c\u4e86\u89e3\u5176\u5468\u8fb9\u7684\u7ed3\u6784\u3002\u5982\u679c<code>p<\/code> \u6807\u7b7e\u5185\u4ec5\u6709\u4e00\u4e2a<code>span<\/code> \u6807\u7b7e\uff0c\u5e76\u4e14\u9700\u8981\u7684\u6587\u672c\u5c31\u5728\u8fd9\u4e24\u4e2a\u6807\u7b7e\u4e4b\u95f4\uff0c\u53ef\u4ee5\u4f7f\u7528BeautifulSoup\u5e93\u4e2d\u7684 <code>next_sibling<\/code> \u6216 <code>previous_sibling<\/code> \u65b9\u6cd5\u6765\u83b7\u53d6\u3002\u5982\u679c<code>span<\/code> \u548c\u8981\u63d0\u53d6\u7684\u6587\u672c\u4f4d\u4e8e\u4e0d\u540c\u7684<code>p<\/code> \u6807\u7b7e\u4e2d\uff0c\u5219\u9700\u8981\u6839\u636e\u6587\u6863\u7ed3\u6784\u4f7f\u7528<code>find<\/code>\u6216<code>find_all<\/code>\u65b9\u6cd5\uff0c\u914d\u5408CSS\u9009\u62e9\u5668\u6216XPath\u6765\u51c6\u786e\u5b9a\u4f4d\u76ee\u6807 <code>p<\/code> \u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4e86\u89e3\u722c\u866b\u548cHTML\u57fa\u7840<\/h2>\n<\/p>\n<p><p>\u5728\u6df1\u5165\u722c\u53d6\u6307\u5b9a\u5b57\u6bb5\u524d\uff0c\u5fc5\u987b\u5148\u638c\u63e1\u7f51\u7edc\u722c\u866b\u7684\u57fa\u672c\u539f\u7406\u4ee5\u53caHTML\u7684\u57fa\u7840\u77e5\u8bc6\u3002\u7f51\u7edc\u722c\u866b\u662f\u4e00\u79cd\u81ea\u52a8\u6293\u53d6\u4e92\u8054\u7f51\u4fe1\u606f\u7684\u7a0b\u5e8f\uff0c\u5b83\u53ef\u4ee5\u6a21\u62df\u6d4f\u89c8\u5668\u8bbf\u95ee\u7f51\u9875\uff0c\u83b7\u53d6\u6240\u9700\u7684\u6570\u636e\u3002HTML\u5219\u662f\u7f51\u9875\u5185\u5bb9\u7684\u6807\u51c6\u6807\u8bb0\u8bed\u8a00\uff0c\u5b9a\u4e49\u4e86\u7f51\u9875\u7684\u7ed3\u6784\u548c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>1.1 \u4ec0\u4e48\u662f\u7f51\u7edc\u722c\u866b<\/h3>\n<\/p>\n<p><p>\u7f51\u7edc\u722c\u866b\uff0c\u4e5f\u79f0\u4e3a\u7f51\u7edc\u673a\u5668\u4eba\u6216\u8718\u86db\uff0c\u5176\u4e3b\u8981\u4f5c\u7528\u662f\u81ea\u52a8\u8bbf\u95ee\u7f51\u7edc\u4e2d\u7684\u591a\u4e2a\u9875\u9762\uff0c\u641c\u96c6\u4fe1\u606f\u5e76\u89e3\u6790\u6570\u636e\u3002\u901a\u8fc7\u53d1\u9001\u8bf7\u6c42\u3001\u63a5\u6536\u54cd\u5e94\u548c\u89e3\u6790\u7f51\u9875\u5185\u5bb9\uff0c\u722c\u866b\u80fd\u591f\u9ad8\u6548\u5730\u6267\u884c\u6570\u636e\u6536\u96c6\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>1.2 HTML\u77e5\u8bc6\u7b80\u4ecb<\/h3>\n<\/p>\n<p><p>HTML\u6587\u6863\u7531\u4e00\u7cfb\u5217\u5d4c\u5957\u7684\u6807\u7b7e\u6784\u6210\uff0c\u4f8b\u5982<code>&lt;html&gt;<\/code>\u3001<code>&lt;body&gt;<\/code>\u3001<code>&lt;div&gt;<\/code>\u3001<code>&lt;span&gt;<\/code>\u548c<code>&lt;p&gt;<\/code>\u7b49\u3002\u6bcf\u4e2a\u6807\u7b7e\u90fd\u627f\u8f7d\u7740\u7279\u5b9a\u7684\u5185\u5bb9\u548c\u542b\u4e49\uff0c\u901a\u8fc7\u8bc6\u522b\u548c\u89e3\u6790\u8fd9\u4e9b\u6807\u7b7e\u4e2d\u7684\u6570\u636e\uff0c\u5c31\u53ef\u4ee5\u8fbe\u5230\u6293\u53d6\u4fe1\u606f\u7684\u76ee\u7684\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u51c6\u5907\u5de5\u4f5c\u548c\u5de5\u5177\u9009\u62e9<\/h2>\n<\/p>\n<p><p>\u5728\u722c\u866b\u5b9e\u8df5\u4e4b\u524d\uff0c\u9700\u8981\u9009\u5b9a\u5408\u9002\u7684\u5de5\u5177\u548c\u5e93\u6765\u8fdb\u884c\u7f51\u9875\u8bf7\u6c42\u548c\u5185\u5bb9\u89e3\u6790\u3002\u6b64\u5916\uff0c\u8fd8\u5e94\u5f53\u786e\u4fdd\u5bf9\u76ee\u6807\u7f51\u9875\u7684\u7ed3\u6784\u6709\u8db3\u591f\u7684\u4e86\u89e3\u3002<\/p>\n<\/p>\n<p><h3>2.1 \u9009\u5b9a\u5408\u9002\u7684\u722c\u866b\u5e93<\/h3>\n<\/p>\n<p><p>\u5e38\u7528\u7684\u7f51\u7edc\u722c\u866b\u5e93\u6709Python\u7684requests\u3001BeautifulSoup\u3001lxml\u548cScrapy\u7b49\u3002\u5176\u4e2d\uff0crequests\u5e93\u7528\u4e8e\u53d1\u9001\u7f51\u7edc\u8bf7\u6c42\uff1bBeautifulSoup\u548clxml\u7528\u4e8e\u89e3\u6790HTML\/XML\u6587\u6863\uff1bScrapy\u5219\u662f\u4e00\u4e2a\u96c6\u6210\u4e86\u591a\u79cd\u529f\u80fd\u7684\u722c\u866b\u6846\u67b6\u3002<\/p>\n<\/p>\n<p><h3>2.2 \u5206\u6790\u76ee\u6807\u7f51\u9875\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u6d4f\u89c8\u5668\u7684\u5f00\u53d1\u8005\u5de5\u5177\u53ef\u4ee5\u67e5\u770b\u5e76\u5206\u6790\u7f51\u9875\u7684DOM\u7ed3\u6784\u3002\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u5e94\u5f53\u8bc6\u522b\u51fa\u5305\u542b\u76ee\u6807\u6570\u636e\u7684<code>p<\/code> \u6807\u7b7e\u53ca\u5176\u5468\u8fb9\u4e0a\u4e0b\u6587\uff0c\u4ee5\u4fbf\u6784\u5efa\u9ad8\u6548\u7684\u9009\u53d6\u89c4\u5219\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u5b9e\u9645\u722c\u53d6\u6b65\u9aa4<\/h2>\n<\/p>\n<p><p>\u5b9e\u9645\u722c\u53d6\u7684\u8fc7\u7a0b\u5206\u4e3a\u591a\u4e2a\u6b65\u9aa4\uff0c\u4ee5\u4e0b\u662f\u5177\u4f53\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>3.1 \u53d1\u9001\u8bf7\u6c42\u548c\u63a5\u6536\u54cd\u5e94<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f7f\u7528requests\u7b49\u5e93\u5411\u76ee\u6807URL\u53d1\u9001HTTP\u8bf7\u6c42\u3002\u5982\u679c\u8bf7\u6c42\u6210\u529f\uff0c\u670d\u52a1\u5668\u5c06\u8fd4\u56de\u5305\u542b\u7f51\u9875\u5185\u5bb9\u7684\u54cd\u5e94\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>3.2 \u89e3\u6790\u548c\u5b9a\u4f4dHTML\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u63a5\u7740\uff0c\u5229\u7528BeautifulSoup\u7b49\u5e93\u8f7d\u5165\u7f51\u9875HTML\u5185\u5bb9\uff0c\u5e76\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u5bf9<code>p<\/code> \u6807\u7b7e\u53ca\u5176\u5185\u90e8\u7684<code>span<\/code> \u6807\u7b7e\u8fdb\u884c\u5b9a\u4f4d\u3002\u8fd9\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u6807\u7b7e\u9009\u62e9\u5668\u3001\u7c7b\u9009\u62e9\u5668\u6216ID\u9009\u62e9\u5668\u7b49\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u63d0\u53d6\u548c\u5904\u7406\u6570\u636e<\/h2>\n<\/p>\n<p><p>\u627e\u5230\u76ee\u6807<code>p<\/code> \u6807\u7b7e\u548c<code>span<\/code> \u6807\u7b7e\u540e\uff0c\u9700\u8981\u5c06\u8fd9\u4e9b\u6570\u636e\u63d0\u53d6\u51fa\u6765\uff0c\u5e76\u5220\u9664\u6216\u6392\u9664\u4e0d\u9700\u8981\u7684\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><h3>4.1 \u63d0\u53d6\u6587\u672c\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528BeautifulSoup\u7684<code>get_text()<\/code>\u6216<code>string<\/code>\u5c5e\u6027\u53ef\u4ee5\u83b7\u53d6\u6807\u7b7e\u5185\u90e8\u7684\u7eaf\u6587\u672c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>4.2 \u6e05\u6d17\u548c\u5b58\u50a8\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u83b7\u53d6\u5230\u7684\u6570\u636e\u53ef\u80fd\u542b\u6709\u7a7a\u683c\u3001\u6362\u884c\u6216\u5176\u4ed6\u975e\u5fc5\u987b\u5143\u7d20\uff0c\u9700\u8981\u8fdb\u884c\u6e05\u6d17\u3002\u6700\u540e\uff0c\u5c06\u6e05\u6d17\u540e\u7684\u6570\u636e\u5b58\u50a8\u5230\u6587\u4ef6\u6216\u6570\u636e\u5e93\u4e2d\u4ee5\u4f9b\u540e\u7eed\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5bf9\u7279\u5b9a\u7f51\u9875\u5143\u7d20\u7684\u722c\u53d6\u548c\u6570\u636e\u5904\u7406\u3002\u91cd\u8981\u7684\u662f\uff0c\u5fc5\u987b\u9075\u5b88\u7f51\u7ad9\u7684Robots\u534f\u8bae\uff0c\u8fdb\u884c\u5408\u6cd5\u548c\u5408\u7406\u7684\u722c\u53d6\u884c\u4e3a\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u4f7f\u7528\u722c\u866b\u722c\u53d6p\u6807\u7b7e\u5185\u7684\u5b57\u6bb5\uff1f<\/strong><\/p>\n<p>\u722c\u53d6p\u6807\u7b7e\u5185\u7684\u5b57\u6bb5\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\u5b9e\u73b0\uff1a\u9996\u5148\uff0c\u4f7f\u7528\u722c\u866b\u6846\u67b6\uff08\u5982Scrapy\uff09\u53d1\u9001HTTP\u8bf7\u6c42\u83b7\u53d6\u7f51\u9875\u7684HTML\u5185\u5bb9\u3002\u7136\u540e\uff0c\u5229\u7528\u89e3\u6790\u5e93\uff08\u5982BeautifulSoup\uff09\u89e3\u6790HTML\u6587\u6863\uff0c\u5e76\u5b9a\u4f4d\u5230\u5305\u542bp\u6807\u7b7e\u7684\u90e8\u5206\u3002\u63a5\u7740\uff0c\u4f7f\u7528\u6807\u7b7e\u9009\u62e9\u5668\u6216Xpath\u7b49\u65b9\u6cd5\uff0c\u63d0\u53d6\u51fa\u9700\u8981\u7684p\u6807\u7b7e\u5185\u7684\u5185\u5bb9\u3002\u6700\u540e\uff0c\u5bf9\u63d0\u53d6\u5230\u7684\u5b57\u6bb5\u8fdb\u884c\u5904\u7406\u548c\u5b58\u50a8\u3002<\/p>\n<p><strong>2. \u600e\u6837\u5229\u7528XPath\u722c\u53d6p\u6807\u7b7e\u5185\u7684\u6587\u672c\u5185\u5bb9\uff1f<\/strong><\/p>\n<p>\u5229\u7528XPath\u6765\u722c\u53d6p\u6807\u7b7e\u5185\u7684\u6587\u672c\u5185\u5bb9\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\uff1a\u9996\u5148\uff0c\u4f7f\u7528\u722c\u866b\u6846\u67b6\u53d1\u9001HTTP\u8bf7\u6c42\u5e76\u83b7\u53d6\u7f51\u9875\u7684HTML\u5185\u5bb9\u3002\u7136\u540e\uff0c\u4f7f\u7528XPath\u8868\u8fbe\u5f0f\u5b9a\u4f4d\u5230p\u6807\u7b7e\u7684\u4f4d\u7f6e\uff0c\u4f8b\u5982<code>\/\/p<\/code>\u5373\u8868\u793a\u9009\u53d6\u6587\u6863\u4e2d\u6240\u6709\u7684p\u6807\u7b7e\u3002\u63a5\u7740\uff0c\u5229\u7528XPath\u7684\u6587\u672c\u63d0\u53d6\u51fd\u6570\uff08\u5982text()\uff09\u63d0\u53d6\u51fap\u6807\u7b7e\u5185\u7684\u6587\u672c\u5185\u5bb9\u3002\u6700\u540e\uff0c\u5bf9\u63d0\u53d6\u5230\u7684\u5b57\u6bb5\u8fdb\u884c\u5fc5\u8981\u7684\u5904\u7406\u548c\u5b58\u50a8\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u722c\u53d6p\u6807\u7b7e\u5185\u7684\u5b57\u6bb5\uff1f<\/strong><\/p>\n<p>\u722c\u53d6p\u6807\u7b7e\u5185\u7684\u5b57\u6bb5\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u5339\u914d\u63d0\u53d6\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a\u9996\u5148\uff0c\u4f7f\u7528\u722c\u866b\u6846\u67b6\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u83b7\u53d6\u7f51\u9875\u7684HTML\u5185\u5bb9\u3002\u7136\u540e\uff0c\u7f16\u5199\u76f8\u5e94\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u89c4\u5219\uff0c\u5339\u914d\u51fap\u6807\u7b7e\u7684\u5185\u5bb9\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>&lt;p&gt;(.*?)&lt;\/p&gt;<\/code>\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914dp\u6807\u7b7e\u5185\u7684\u5185\u5bb9\u3002\u63a5\u7740\uff0c\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u51fd\u6570\uff0c\u63d0\u53d6\u51fa\u7b26\u5408\u89c4\u5219\u7684\u5185\u5bb9\u3002\u6700\u540e\uff0c\u5bf9\u63d0\u53d6\u7684\u5b57\u6bb5\u8fdb\u884c\u5fc5\u8981\u7684\u5904\u7406\u548c\u5b58\u50a8\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u722c\u53d6\u7f51\u9875\u4e2d\u7279\u5b9a\u7684\u5b57\u6bb5\u901a\u5e38\u9700\u8981\u4f7f\u7528\u7f51\u7edc\u722c\u866b\uff0c\u5e76\u5bf9HTML\u5143\u7d20\u8fdb\u884c\u67e5\u8be2\u548c\u89e3\u6790\u3002\u5bf9\u4e8e\u4e00\u4e2a\u4f4d\u4e8espan \u6807\u7b7e\u5916\u9762\u3001p [&hellip;]","protected":false},"author":3,"featured_media":232060,"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\/232057"}],"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=232057"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/232057\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/232060"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=232057"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=232057"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=232057"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}