{"id":949585,"date":"2024-12-27T00:14:16","date_gmt":"2024-12-26T16:14:16","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/949585.html"},"modified":"2024-12-27T00:14:19","modified_gmt":"2024-12-26T16:14:19","slug":"python%e5%a6%82%e4%bd%95%e5%ae%89%e8%a3%85beautiful","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/949585.html","title":{"rendered":"python\u5982\u4f55\u5b89\u88c5beautiful"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084517\/1cbafac8-9e89-4a34-8252-d5b62677b2d2.webp\" alt=\"python\u5982\u4f55\u5b89\u88c5beautiful\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5b89\u88c5Beautiful Soup\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b8c\u6210\uff1a\u4f7f\u7528pip\u547d\u4ee4\u3001\u786e\u4fddPython\u73af\u5883\u6b63\u786e\u3001\u9a8c\u8bc1\u5b89\u88c5\u3002<\/strong>\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u5bf9\u5176\u4e2d\u7684\u201c\u4f7f\u7528pip\u547d\u4ee4\u201d\u8fdb\u884c\u8be6\u7ec6\u63cf\u8ff0\u3002Pip\u662fPython\u7684\u5305\u7ba1\u7406\u5668\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8f7b\u677e\u5730\u5b89\u88c5\u548c\u7ba1\u7406Python\u5305\u3002\u5728\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\u4e2d\u8f93\u5165<code>pip install beautifulsoup4<\/code>\u5373\u53ef\u5b89\u88c5Beautiful Soup\u3002\u8fd9\u4f1a\u4ecePython Package Index (PyPI) \u4e0b\u8f7d\u5e76\u5b89\u88c5\u6700\u65b0\u7248\u672c\u7684Beautiful Soup\u3002\u6b64\u5916\uff0c\u5982\u679c\u60a8\u4f7f\u7528\u7684\u662fAnaconda\u73af\u5883\uff0c\u5219\u53ef\u4ee5\u901a\u8fc7<code>conda install -c anaconda beautifulsoup4<\/code>\u6765\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528PIP\u547d\u4ee4<\/p>\n<\/p>\n<p><p>PIP\u662fPython\u7684\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u5b83\u4f7f\u5f97\u5b89\u88c5Python\u5e93\u53d8\u5f97\u975e\u5e38\u7b80\u5355\u3002\u901a\u8fc7PIP\u547d\u4ee4\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5b89\u88c5Beautiful Soup\u5e76\u786e\u4fdd\u5b83\u7684\u7248\u672c\u662f\u6700\u65b0\u7684\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u6253\u5f00\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\uff1a\u5728Windows\u4e0a\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u6309Win + R\u5e76\u8f93\u5165cmd\u6765\u6253\u5f00\u547d\u4ee4\u884c\u3002\u5728MacOS\u6216Linux\u4e0a\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u6253\u5f00\u7ec8\u7aef\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u8f93\u5165\u5b89\u88c5\u547d\u4ee4\uff1a\u5728\u547d\u4ee4\u884c\u4e2d\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5Beautiful Soup\uff1a<\/p>\n<\/p>\n<p><pre><code>pip install beautifulsoup4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u81ea\u52a8\u4ecePython Package Index (PyPI) \u4e0b\u8f7d\u5e76\u5b89\u88c5Beautiful Soup\u7684\u6700\u65b0\u7248\u672c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u9a8c\u8bc1\u5b89\u88c5\uff1a\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u9a8c\u8bc1Beautiful Soup\u662f\u5426\u6b63\u786e\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">python -c &quot;import bs4; print(bs4.__version__)&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u6ca1\u6709\u9519\u8bef\uff0c\u5e76\u4e14\u6253\u5370\u51fa\u7248\u672c\u53f7\uff0c\u8bf4\u660e\u5b89\u88c5\u6210\u529f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u786e\u4fddPython\u73af\u5883\u6b63\u786e<\/p>\n<\/p>\n<p><p>\u5728\u5b89\u88c5Beautiful Soup\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u662f\u8bbe\u7f6e\u6b63\u786e\u7684\u3002\u8fd9\u610f\u5473\u7740\u4f60\u9700\u8981\u5b89\u88c5Python\uff0c\u5e76\u4e14\u914d\u7f6e\u597d\u73af\u5883\u53d8\u91cf\u4ee5\u4fbf\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528Python\u548cPIP\u547d\u4ee4\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u68c0\u67e5Python\u7248\u672c\uff1a\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165<code>python --version<\/code>\uff0c\u786e\u4fdd\u4f60\u5b89\u88c5\u4e86Python 3.x\u7248\u672c\uff0c\u56e0\u4e3aBeautiful Soup\u652f\u6301Python 3\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff1a\u5728Windows\u4e0a\uff0c\u786e\u4fddPython\u7684\u5b89\u88c5\u76ee\u5f55\u88ab\u6dfb\u52a0\u5230\u73af\u5883\u53d8\u91cfPATH\u4e2d\uff0c\u4ee5\u4fbf\u547d\u4ee4\u884c\u53ef\u4ee5\u8bc6\u522bPython\u547d\u4ee4\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u66f4\u65b0PIP\uff1a\u6709\u65f6\u5019PIP\u7248\u672c\u8fc7\u65e7\u53ef\u80fd\u5bfc\u81f4\u5b89\u88c5\u95ee\u9898\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u66f4\u65b0PIP\uff1a<\/p>\n<\/p>\n<p><pre><code>python -m pip install --upgrade pip<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u9a8c\u8bc1\u5b89\u88c5<\/p>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u9a8c\u8bc1\u662f\u4e00\u4e2a\u91cd\u8981\u6b65\u9aa4\uff0c\u4ee5\u786e\u4fddBeautiful Soup\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\u3002\u9664\u4e86\u5728\u547d\u4ee4\u884c\u4e2d\u68c0\u67e5\u7248\u672c\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684Python\u811a\u672c\u6765\u6d4b\u8bd5Beautiful Soup\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u521b\u5efa\u4e00\u4e2a\u6d4b\u8bd5\u6587\u4ef6\uff1a\u4f7f\u7528\u6587\u672c\u7f16\u8f91\u5668\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\uff0c\u6bd4\u5982test_bs4.py\uff0c\u5e76\u8f93\u5165\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>html_doc = &quot;&lt;html&gt;&lt;head&gt;&lt;title&gt;The Dormouse&#39;s story&lt;\/title&gt;&lt;\/head&gt;&lt;body&gt;&lt;p class=&#39;title&#39;&gt;&lt;b&gt;The Dormouse&#39;s story&lt;\/b&gt;&lt;\/p&gt;&lt;\/body&gt;&lt;\/html&gt;&quot;<\/p>\n<p>soup = BeautifulSoup(html_doc, &#39;html.parser&#39;)<\/p>\n<p>print(soup.title.string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u8fd0\u884c\u6d4b\u8bd5\u6587\u4ef6\uff1a\u5728\u547d\u4ee4\u884c\u4e2d\uff0c\u5bfc\u822a\u5230\u4fdd\u5b58test_bs4.py\u7684\u76ee\u5f55\uff0c\u7136\u540e\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code>python test_bs4.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u8f93\u51fa\u4e3a&quot;The Dormouse&#39;s story&quot;\uff0c\u8bf4\u660eBeautiful Soup\u5de5\u4f5c\u6b63\u5e38\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u4f7f\u7528ANACONDA\u73af\u5883<\/p>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662fAnaconda\u73af\u5883\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5b89\u88c5\u548c\u7ba1\u7406Python\u5305\uff0c\u5305\u62ecBeautiful Soup\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u6253\u5f00Anaconda Prompt\uff1a\u5728Windows\u4e0a\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u5f00\u59cb\u83dc\u5355\u627e\u5230Anaconda Prompt\u3002\u5728MacOS\u6216Linux\u4e0a\uff0c\u5728\u7ec8\u7aef\u4e2d\u8f93\u5165<code>anaconda-navigator<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u521b\u5efa\u865a\u62df\u73af\u5883\uff08\u53ef\u9009\uff09\uff1a\u4e3a\u4e86\u4fdd\u6301\u9879\u76ee\u7684\u4f9d\u8d56\u6027\u5e72\u51c0\u5e76\u907f\u514d\u7248\u672c\u51b2\u7a81\uff0c\u5efa\u8bae\u4e3a\u6bcf\u4e2a\u9879\u76ee\u521b\u5efa\u4e00\u4e2a\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code>conda create -n myenv python=3.8<\/p>\n<p>conda activate myenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528Conda\u5b89\u88c5Beautiful Soup\uff1a\u5728\u6fc0\u6d3b\u7684\u73af\u5883\u4e2d\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5Beautiful Soup\uff1a<\/p>\n<\/p>\n<p><pre><code>conda install -c anaconda beautifulsoup4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u9a8c\u8bc1\u5b89\u88c5\uff1a\u540c\u6837\u5730\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884c\u6d4b\u8bd5\u811a\u672c\u6765\u9a8c\u8bc1Beautiful Soup\u662f\u5426\u5b89\u88c5\u6b63\u786e\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u5904\u7406\u5b89\u88c5\u95ee\u9898<\/p>\n<\/p>\n<p><p>\u5728\u5b89\u88c5\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\uff0c\u6bd4\u5982\u7f51\u7edc\u95ee\u9898\u3001\u6743\u9650\u95ee\u9898\u6216\u7248\u672c\u4e0d\u517c\u5bb9\u95ee\u9898\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u7f51\u7edc\u95ee\u9898\uff1a\u5982\u679c\u5b89\u88c5\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u7f51\u7edc\u9519\u8bef\uff0c\u53ef\u80fd\u662f\u56e0\u4e3a\u8bbf\u95eePyPI\u53d7\u9650\u3002\u4f60\u53ef\u4ee5\u5c1d\u8bd5\u4f7f\u7528\u56fd\u5185\u7684\u955c\u50cf\u6e90\uff0c\u6bd4\u5982\u6e05\u534e\u5927\u5b66\u7684\u6e90\uff1a<\/p>\n<\/p>\n<p><pre><code>pip install beautifulsoup4 -i https:\/\/pypi.tuna.tsinghua.edu.cn\/simple<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u6743\u9650\u95ee\u9898\uff1a\u5982\u679c\u5728Windows\u4e0a\u9047\u5230\u6743\u9650\u95ee\u9898\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u8fd0\u884c\u547d\u4ee4\u884c\uff0c\u6216\u8005\u4f7f\u7528<code>--user<\/code>\u9009\u9879\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code>pip install --user beautifulsoup4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u7248\u672c\u4e0d\u517c\u5bb9\uff1a\u786e\u4fdd\u4f60\u7684Python\u7248\u672c\u4e0eBeautiful Soup\u517c\u5bb9\uff0c\u901a\u5e38Python 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\u90fd\u53ef\u4ee5\u6b63\u5e38\u652f\u6301\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u5e94\u8be5\u80fd\u591f\u5728Python\u4e2d\u6210\u529f\u5b89\u88c5\u548c\u4f7f\u7528Beautiful Soup\u8fdb\u884c\u7f51\u9875\u89e3\u6790\u548c\u6570\u636e\u63d0\u53d6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b89\u88c5Beautiful Soup\u5e93\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u5b89\u88c5Beautiful Soup\u5e93\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5305\u7ba1\u7406\u5de5\u5177pip\u3002\u6253\u5f00\u547d\u4ee4\u884c\u754c\u9762\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<code>pip install beautifulsoup4<\/code>\u3002\u8fd9\u5c06\u4f1a\u4e0b\u8f7d\u5e76\u5b89\u88c5\u6700\u65b0\u7248\u672c\u7684Beautiful Soup\u5e93\u5230\u60a8\u7684Python\u73af\u5883\u4e2d\u3002<\/p>\n<p><strong>Beautiful Soup\u5e93\u7684\u4e3b\u8981\u7528\u9014\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>Beautiful Soup\u662f\u4e00\u4e2a\u7528\u4e8e\u89e3\u6790HTML\u548cXML\u6587\u6863\u7684Python\u5e93\u3002\u5b83\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u4ece\u7f51\u9875\u4e2d\u63d0\u53d6\u6570\u636e\uff0c\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u548c\u5904\u7406\u3002\u65e0\u8bba\u662f\u8fdb\u884c\u7f51\u9875\u6293\u53d6\uff0c\u8fd8\u662f\u5904\u7406API\u8fd4\u56de\u7684HTML\u5185\u5bb9\uff0cBeautiful Soup\u90fd\u80fd\u63d0\u4f9b\u6709\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<p><strong>\u5b89\u88c5Beautiful Soup\u540e\uff0c\u5982\u4f55\u9a8c\u8bc1\u5b89\u88c5\u662f\u5426\u6210\u529f\uff1f<\/strong><br \/>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5728Python\u89e3\u91ca\u5668\u4e2d\u8f93\u5165\u4ee5\u4e0b\u4ee3\u7801\u6765\u9a8c\u8bc1\u5b89\u88c5\u662f\u5426\u6210\u529f\uff1a  <\/p>\n<pre><code class=\"language-python\">from bs4 import BeautifulSoup  \nprint(BeautifulSoup(&quot;&lt;html&gt;&lt;\/html&gt;&quot;, &quot;html.parser&quot;))  \n<\/code><\/pre>\n<p>\u5982\u679c\u6ca1\u6709\u51fa\u73b0\u9519\u8bef\uff0c\u5e76\u4e14\u60a8\u770b\u5230\u4e86\u4e00\u4e2aBeautifulSoup\u5bf9\u8c61\u7684\u8f93\u51fa\uff0c\u8bf4\u660e\u5b89\u88c5\u6210\u529f\u3002<\/p>\n<p><strong>\u4f7f\u7528Beautiful Soup\u8fdb\u884c\u7f51\u9875\u6293\u53d6\u7684\u57fa\u672c\u6b65\u9aa4\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u4f7f\u7528Beautiful Soup\u8fdb\u884c\u7f51\u9875\u6293\u53d6\u7684\u57fa\u672c\u6b65\u9aa4\u5305\u62ec\uff1a  <\/p>\n<ol>\n<li>\u4f7f\u7528requests\u5e93\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\u3002  <\/li>\n<li>\u4f7f\u7528Beautiful Soup\u89e3\u6790\u8fd4\u56de\u7684HTML\u6587\u6863\u3002  <\/li>\n<li>\u4f7f\u7528Beautiful Soup\u63d0\u4f9b\u7684\u65b9\u6cd5\u67e5\u627e\u5e76\u63d0\u53d6\u6240\u9700\u7684\u6570\u636e\u3002  <\/li>\n<li>\u5c06\u63d0\u53d6\u7684\u6570\u636e\u8fdb\u884c\u5b58\u50a8\u6216\u8fdb\u4e00\u6b65\u5904\u7406\u3002<br \/>\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u4ece\u7f51\u9875\u4e2d\u83b7\u53d6\u4fe1\u606f\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5b89\u88c5Beautiful Soup\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b8c\u6210\uff1a\u4f7f\u7528pip\u547d\u4ee4\u3001\u786e\u4fddPython\u73af\u5883\u6b63\u786e [&hellip;]","protected":false},"author":3,"featured_media":949589,"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\/949585"}],"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=949585"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/949585\/revisions"}],"predecessor-version":[{"id":949594,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/949585\/revisions\/949594"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/949589"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=949585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=949585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=949585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}