{"id":1048251,"date":"2024-12-31T13:49:26","date_gmt":"2024-12-31T05:49:26","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1048251.html"},"modified":"2024-12-31T13:49:28","modified_gmt":"2024-12-31T05:49:28","slug":"python3-%e5%a6%82%e4%bd%95%e5%ae%89%e8%a3%85bs4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1048251.html","title":{"rendered":"python3 \u5982\u4f55\u5b89\u88c5bs4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/ffe6657e-6ad7-4864-b80b-5c8fe5fbca0a.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python3 \u5982\u4f55\u5b89\u88c5bs4\" \/><\/p>\n<p><p> <strong>\u5b89\u88c5 BeautifulSoup4 (bs4) \u7684\u4e3b\u8981\u6b65\u9aa4\u6709\uff1a\u786e\u4fdd\u5df2\u5b89\u88c5 pip\u3001\u4f7f\u7528 pip \u5b89\u88c5 bs4\u3001\u9a8c\u8bc1\u5b89\u88c5\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u8be6\u7ec6\u7684\u6b65\u9aa4\u662f\u4f7f\u7528 pip \u5b89\u88c5 bs4\u3002\u5177\u4f53\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u786e\u4fdd\u5df2\u5b89\u88c5 pip<\/p>\n<p>\u5728\u5b89\u88c5 BeautifulSoup4 \u4e4b\u524d\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u4f60\u7684\u7cfb\u7edf\u5df2\u7ecf\u5b89\u88c5\u4e86 pip\uff0c\u8fd9\u662f Python \u7684\u5305\u7ba1\u7406\u5de5\u5177\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6765\u68c0\u67e5 pip \u662f\u5426\u5df2\u7ecf\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u6ca1\u6709\u5b89\u88c5 pip\uff0c\u53ef\u4ee5\u6839\u636e\u4f60\u7684\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u5b89\u88c5\u3002\u5bf9\u4e8e Windows \u7528\u6237\uff0c\u53ef\u4ee5\u4e0b\u8f7d\u5e76\u8fd0\u884c get-pip.py \u811a\u672c\u3002\u5bf9\u4e8e Mac \u548c Linux \u7528\u6237\uff0cpip \u901a\u5e38\u662f\u9ed8\u8ba4\u5b89\u88c5\u7684\uff0c\u4f46\u5982\u679c\u6ca1\u6709\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5305\u7ba1\u7406\u5668\u5982 Homebrew \u6216\u8005\u76f4\u63a5\u4ece Python \u5b98\u65b9\u7f51\u7ad9\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528 pip \u5b89\u88c5 bs4<\/p>\n<p>\u5728\u786e\u8ba4 pip \u5df2\u7ecf\u5b89\u88c5\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 pip \u6765\u5b89\u88c5 BeautifulSoup4\u3002\u53ea\u9700\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install beautifulsoup4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u81ea\u52a8\u4e0b\u8f7d\u5e76\u5b89\u88c5 BeautifulSoup4 \u53ca\u5176\u6240\u6709\u4f9d\u8d56\u9879\u3002\u5b89\u88c5\u8fc7\u7a0b\u901a\u5e38\u4f1a\u975e\u5e38\u5feb\uff0c\u51e0\u79d2\u949f\u5185\u5373\u53ef\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u9a8c\u8bc1\u5b89\u88c5<\/p>\n<p>\u5728\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u5728 Python \u4ea4\u4e92\u5f0f\u547d\u4ee4\u884c\u6216\u811a\u672c\u4e2d\u5bfc\u5165 BeautifulSoup \u6765\u9a8c\u8bc1\u5b89\u88c5\u662f\u5426\u6210\u529f\u3002\u6253\u5f00 Python \u89e3\u91ca\u5668\u5e76\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u6ca1\u6709\u770b\u5230\u9519\u8bef\u6d88\u606f\uff0c\u8bf4\u660e BeautifulSoup4 \u5df2\u7ecf\u6210\u529f\u5b89\u88c5\u5e76\u53ef\u4ee5\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u786e\u4fdd\u5df2\u5b89\u88c5 pip<\/h3>\n<\/p>\n<p><p>\u5728\u5b89\u88c5\u4efb\u4f55 Python \u5305\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86 pip\uff0c\u8fd9\u662f Python \u7684\u5305\u7ba1\u7406\u5de5\u5177\u3002pip \u662f\u4e00\u4e2a\u9012\u5f52\u7f29\u5199\uff0c\u610f\u601d\u662f &quot;Pip Installs Packages&quot;\u3002\u5b83\u662f Python \u7f16\u7a0b\u4e2d\u6700\u5e38\u7528\u7684\u5de5\u5177\u4e4b\u4e00\u3002\u4ee5\u4e0b\u662f\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u786e\u4fdd\u5b89\u88c5 pip \u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1. Windows \u7cfb\u7edf<\/h4>\n<\/p>\n<p><p>Windows \u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b89\u88c5 pip\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u8bbf\u95ee Python \u5b98\u65b9\u7f51\u7ad9\u5e76\u4e0b\u8f7d\u6700\u65b0\u7248\u672c\u7684 Python \u5b89\u88c5\u7a0b\u5e8f\u3002<\/li>\n<li>\u8fd0\u884c\u5b89\u88c5\u7a0b\u5e8f\uff0c\u5e76\u786e\u4fdd\u9009\u4e2d\u201cAdd Python to PATH\u201d\u9009\u9879\u3002<\/li>\n<li>\u5728\u5b89\u88c5\u8fc7\u7a0b\u4e2d\uff0c\u9009\u62e9\u201cInstall pip\u201d\u9009\u9879\u3002<\/li>\n<\/ol>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6765\u9a8c\u8bc1 pip \u662f\u5426\u5df2\u7ecf\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u770b\u5230 pip \u7684\u7248\u672c\u4fe1\u606f\uff0c\u8bf4\u660e pip \u5df2\u7ecf\u6210\u529f\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><h4>2. macOS \u7cfb\u7edf<\/h4>\n<\/p>\n<p><p>macOS \u7528\u6237\u901a\u5e38\u5df2\u7ecf\u9ed8\u8ba4\u5b89\u88c5\u4e86 Python \u548c pip\u3002\u5982\u679c\u6ca1\u6709\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00\u7ec8\u7aef\u3002<\/li>\n<li>\u4f7f\u7528 Homebrew \u5b89\u88c5 Python \u548c pip\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">brew install python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u9a8c\u8bc1 pip \u662f\u5426\u5df2\u7ecf\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. Linux \u7cfb\u7edf<\/h4>\n<\/p>\n<p><p>\u5927\u591a\u6570 Linux \u53d1\u884c\u7248\u9ed8\u8ba4\u5df2\u7ecf\u5b89\u88c5\u4e86 Python \u548c pip\u3002\u5982\u679c\u6ca1\u6709\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00\u7ec8\u7aef\u3002<\/li>\n<li>\u4f7f\u7528\u5305\u7ba1\u7406\u5668\u5b89\u88c5 pip\uff0c\u4f8b\u5982\u5bf9\u4e8e\u57fa\u4e8e Debian \u7684\u7cfb\u7edf\uff0c\u53ef\u4ee5\u4f7f\u7528 apt-get\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">sudo apt-get install python3-pip<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u9a8c\u8bc1 pip \u662f\u5426\u5df2\u7ecf\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 pip \u5b89\u88c5 bs4<\/h3>\n<\/p>\n<p><p>\u5728\u786e\u8ba4\u5df2\u7ecf\u5b89\u88c5 pip \u540e\uff0c\u53ef\u4ee5\u4f7f\u7528 pip \u5b89\u88c5 BeautifulSoup4\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1. \u6253\u5f00\u547d\u4ee4\u884c\u6216\u7ec8\u7aef<\/h4>\n<\/p>\n<p><p>\u5728 Windows \u7cfb\u7edf\u4e0a\uff0c\u6253\u5f00\u547d\u4ee4\u63d0\u793a\u7b26\u6216 PowerShell\u3002\u5728 macOS \u6216 Linux \u7cfb\u7edf\u4e0a\uff0c\u6253\u5f00\u7ec8\u7aef\u3002<\/p>\n<\/p>\n<p><h4>2. \u8f93\u5165\u5b89\u88c5\u547d\u4ee4<\/h4>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff0c\u5b89\u88c5 BeautifulSoup4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install beautifulsoup4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u81ea\u52a8\u4e0b\u8f7d\u5e76\u5b89\u88c5 BeautifulSoup4 \u53ca\u5176\u6240\u6709\u4f9d\u8d56\u9879\u3002\u5b89\u88c5\u8fc7\u7a0b\u901a\u5e38\u975e\u5e38\u5feb\uff0c\u51e0\u79d2\u949f\u5185\u5373\u53ef\u5b8c\u6210\u3002\u4ee5\u4e0b\u662f\u5b89\u88c5\u8fc7\u7a0b\u7684\u8f93\u51fa\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">Collecting beautifulsoup4<\/p>\n<p>  Downloading beautifulsoup4-4.10.0-py3-none-any.whl (97 kB)<\/p>\n<p>Installing collected packages: beautifulsoup4<\/p>\n<p>Successfully installed beautifulsoup4-4.10.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5904\u7406\u4f9d\u8d56\u9879<\/h4>\n<\/p>\n<p><p>BeautifulSoup4 \u4f9d\u8d56\u4e8e\u4e00\u4e2a HTML \u89e3\u6790\u5668\u5e93\uff0c\u5982 lxml \u6216 html5lib\u3002\u867d\u7136 bs4 \u53ef\u4ee5\u4f7f\u7528 Python \u5185\u7f6e\u7684\u89e3\u6790\u5668\uff0c\u4f46\u4e3a\u4e86\u83b7\u5f97\u66f4\u597d\u7684\u6027\u80fd\u548c\u517c\u5bb9\u6027\uff0c\u5efa\u8bae\u5b89\u88c5 lxml \u6216 html5lib\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install lxml<\/p>\n<p>pip install html5lib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u9a8c\u8bc1\u5b89\u88c5<\/h3>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728 Python \u4ea4\u4e92\u5f0f\u547d\u4ee4\u884c\u6216\u811a\u672c\u4e2d\u5bfc\u5165 BeautifulSoup \u6765\u9a8c\u8bc1\u5b89\u88c5\u662f\u5426\u6210\u529f\u3002<\/p>\n<\/p>\n<p><h4>1. \u6253\u5f00 Python \u89e3\u91ca\u5668<\/h4>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff0c\u6253\u5f00 Python \u89e3\u91ca\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5bfc\u5165 BeautifulSoup<\/h4>\n<\/p>\n<p><p>\u5728 Python \u89e3\u91ca\u5668\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff0c\u5bfc\u5165 BeautifulSoup\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u6ca1\u6709\u770b\u5230\u4efb\u4f55\u9519\u8bef\u6d88\u606f\uff0c\u8bf4\u660e BeautifulSoup4 \u5df2\u7ecf\u6210\u529f\u5b89\u88c5\u5e76\u53ef\u4ee5\u4f7f\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528 BeautifulSoup4 \u89e3\u6790 HTML \u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>html_doc = &quot;&quot;&quot;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;title&gt;The Dormouse&#39;s story&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;p class=&quot;title&quot;&gt;&lt;b&gt;The Dormouse&#39;s story&lt;\/b&gt;&lt;\/p&gt;<\/p>\n<p>    &lt;p class=&quot;story&quot;&gt;Once upon a time there were three little sisters; and their names were<\/p>\n<p>        &lt;a href=&quot;http:\/\/example.com\/elsie&quot; class=&quot;sister&quot; id=&quot;link1&quot;&gt;Elsie&lt;\/a&gt;,<\/p>\n<p>        &lt;a href=&quot;http:\/\/example.com\/lacie&quot; class=&quot;sister&quot; id=&quot;link2&quot;&gt;Lacie&lt;\/a&gt; and<\/p>\n<p>        &lt;a href=&quot;http:\/\/example.com\/tillie&quot; class=&quot;sister&quot; id=&quot;link3&quot;&gt;Tillie&lt;\/a&gt;;<\/p>\n<p>        and they lived at the bottom of a well.&lt;\/p&gt;<\/p>\n<p>    &lt;p class=&quot;story&quot;&gt;...&lt;\/p&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>soup = BeautifulSoup(html_doc, &#39;html.parser&#39;)<\/p>\n<p>print(soup.prettify())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u5b89\u88c5 BeautifulSoup4 \u7684\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\u53ca\u5176\u89e3\u51b3\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>1. \u6743\u9650\u95ee\u9898<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u7cfb\u7edf\u4e0a\uff0c\u5b89\u88c5 Python \u5305\u53ef\u80fd\u9700\u8981\u7ba1\u7406\u5458\u6743\u9650\u3002\u5982\u679c\u5728\u5b89\u88c5\u8fc7\u7a0b\u4e2d\u9047\u5230\u6743\u9650\u9519\u8bef\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo pip install beautifulsoup4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u7f51\u7edc\u95ee\u9898<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7f51\u7edc\u8fde\u63a5\u95ee\u9898\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5b89\u88c5\u5931\u8d25\u3002\u53ef\u4ee5\u5c1d\u8bd5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff0c\u6307\u5b9a\u4e00\u4e2a\u4e0d\u540c\u7684\u6e90\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install beautifulsoup4 -i https:\/\/pypi.tuna.tsinghua.edu.cn\/simple<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u7248\u672c\u517c\u5bb9\u6027\u95ee\u9898<\/h4>\n<\/p>\n<p><p>\u786e\u4fdd\u4f60\u7684 Python \u7248\u672c\u4e0e BeautifulSoup4 \u517c\u5bb9\u3002BeautifulSoup4 \u652f\u6301 Python 2.7 \u53ca\u4ee5\u4e0a\u7248\u672c\uff0c\u4f46\u5efa\u8bae\u4f7f\u7528 Python 3.x \u7248\u672c\u4ee5\u83b7\u5f97\u66f4\u597d\u7684\u6027\u80fd\u548c\u517c\u5bb9\u6027\u3002<\/p>\n<\/p>\n<p><h4>4. \u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u5efa\u8bae\u5728\u865a\u62df\u73af\u5883\u4e2d\u5b89\u88c5 BeautifulSoup4 \u53ca\u5176\u4ed6 Python \u5305\uff0c\u4ee5\u907f\u514d\u4e0e\u7cfb\u7edf\u5168\u5c40\u5305\u7684\u51b2\u7a81\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u521b\u5efa\u548c\u6fc0\u6d3b\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python -m venv myenv<\/p>\n<p>source myenv\/bin\/activate  # On Windows: myenv\\Scripts\\activate<\/p>\n<p>pip install beautifulsoup4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528 BeautifulSoup4 \u8fdb\u884c\u57fa\u672c\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5b89\u88c5\u5e76\u9a8c\u8bc1 BeautifulSoup4 \u540e\uff0c\u53ef\u4ee5\u5f00\u59cb\u4f7f\u7528\u5b83\u8fdb\u884c HTML \u548c XML \u7684\u89e3\u6790\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u57fa\u672c\u7528\u6cd5\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1. \u89e3\u6790 HTML \u6587\u6863<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u5982\u4f55\u4f7f\u7528 BeautifulSoup4 \u89e3\u6790\u4e00\u4e2a\u7b80\u5355\u7684 HTML \u6587\u6863\uff0c\u5e76\u63d0\u53d6\u5176\u4e2d\u7684\u6807\u9898\u548c\u94fe\u63a5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>html_doc = &quot;&quot;&quot;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;title&gt;The Dormouse&#39;s story&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;p class=&quot;title&quot;&gt;&lt;b&gt;The Dormouse&#39;s story&lt;\/b&gt;&lt;\/p&gt;<\/p>\n<p>    &lt;p class=&quot;story&quot;&gt;Once upon a time there were three little sisters; and their names were<\/p>\n<p>        &lt;a href=&quot;http:\/\/example.com\/elsie&quot; class=&quot;sister&quot; id=&quot;link1&quot;&gt;Elsie&lt;\/a&gt;,<\/p>\n<p>        &lt;a href=&quot;http:\/\/example.com\/lacie&quot; class=&quot;sister&quot; id=&quot;link2&quot;&gt;Lacie&lt;\/a&gt; and<\/p>\n<p>        &lt;a href=&quot;http:\/\/example.com\/tillie&quot; class=&quot;sister&quot; id=&quot;link3&quot;&gt;Tillie&lt;\/a&gt;;<\/p>\n<p>        and they lived at the bottom of a well.&lt;\/p&gt;<\/p>\n<p>    &lt;p class=&quot;story&quot;&gt;...&lt;\/p&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>soup = BeautifulSoup(html_doc, &#39;html.parser&#39;)<\/p>\n<h2><strong>\u63d0\u53d6\u6807\u9898<\/strong><\/h2>\n<p>title = soup.title.string<\/p>\n<p>print(f&quot;Title: {title}&quot;)<\/p>\n<h2><strong>\u63d0\u53d6\u6240\u6709\u94fe\u63a5<\/strong><\/h2>\n<p>for link in soup.find_all(&#39;a&#39;):<\/p>\n<p>    print(f&quot;Link: {link.get(&#39;href&#39;)} - Text: {link.string}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u89e3\u6790 XML \u6587\u6863<\/h4>\n<\/p>\n<p><p>BeautifulSoup4 \u4e5f\u53ef\u4ee5\u7528\u4e8e\u89e3\u6790 XML \u6587\u6863\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u5982\u4f55\u89e3\u6790\u4e00\u4e2a\u7b80\u5355\u7684 XML \u6587\u6863\uff0c\u5e76\u63d0\u53d6\u5176\u4e2d\u7684\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>xml_doc = &quot;&quot;&quot;<\/p>\n<p>&lt;catalog&gt;<\/p>\n<p>   &lt;book id=&quot;bk101&quot;&gt;<\/p>\n<p>      &lt;author&gt;Gambardella, Matthew&lt;\/author&gt;<\/p>\n<p>      &lt;title&gt;XML Developer&#39;s Guide&lt;\/title&gt;<\/p>\n<p>      &lt;genre&gt;Computer&lt;\/genre&gt;<\/p>\n<p>      &lt;price&gt;44.95&lt;\/price&gt;<\/p>\n<p>      &lt;publish_date&gt;2000-10-01&lt;\/publish_date&gt;<\/p>\n<p>      &lt;description&gt;An in-depth look at creating applications with XML.&lt;\/description&gt;<\/p>\n<p>   &lt;\/book&gt;<\/p>\n<p>   &lt;book id=&quot;bk102&quot;&gt;<\/p>\n<p>      &lt;author&gt;Ralls, Kim&lt;\/author&gt;<\/p>\n<p>      &lt;title&gt;Midnight R<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n&lt;\/title&gt;<\/p>\n<p>      &lt;genre&gt;Fantasy&lt;\/genre&gt;<\/p>\n<p>      &lt;price&gt;5.95&lt;\/price&gt;<\/p>\n<p>      &lt;publish_date&gt;2000-12-16&lt;\/publish_date&gt;<\/p>\n<p>      &lt;description&gt;A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.&lt;\/description&gt;<\/p>\n<p>   &lt;\/book&gt;<\/p>\n<p>&lt;\/catalog&gt;<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>soup = BeautifulSoup(xml_doc, &#39;xml&#39;)<\/p>\n<h2><strong>\u63d0\u53d6\u6240\u6709\u4e66\u7c4d\u7684\u6807\u9898<\/strong><\/h2>\n<p>for book in soup.find_all(&#39;book&#39;):<\/p>\n<p>    title = book.find(&#39;title&#39;).string<\/p>\n<p>    author = book.find(&#39;author&#39;).string<\/p>\n<p>    print(f&quot;Title: {title} - Author: {author}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\u548c\u793a\u4f8b\uff0c\u4f60\u53ef\u4ee5\u987a\u5229\u5b89\u88c5\u548c\u4f7f\u7528 BeautifulSoup4 \u8fdb\u884c HTML \u548c XML \u7684\u89e3\u6790\u3002BeautifulSoup4 \u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\u7684\u89e3\u6790\u5e93\uff0c\u9002\u5408\u5404\u79cd\u7f51\u9875\u6293\u53d6\u548c\u6570\u636e\u89e3\u6790\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python3\u4e2d\u5b89\u88c5Beautiful Soup 4\uff08bs4\uff09\uff1f<\/strong><br \/>\u8981\u5728Python3\u4e2d\u5b89\u88c5Beautiful Soup 4\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5305\u7ba1\u7406\u5de5\u5177pip\u3002\u6253\u5f00\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a  <\/p>\n<pre><code>pip install beautifulsoup4\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u81ea\u52a8\u4e0b\u8f7d\u5e76\u5b89\u88c5bs4\u53ca\u5176\u4f9d\u8d56\u9879\u3002\u5982\u679c\u4f60\u9047\u5230\u6743\u9650\u95ee\u9898\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u5728\u547d\u4ee4\u524d\u52a0\u4e0a<code>sudo<\/code>\uff08\u9002\u7528\u4e8eMac\u548cLinux\uff09\uff0c\u6216\u8005\u4f7f\u7528\u7ba1\u7406\u5458\u6743\u9650\u6253\u5f00\u547d\u4ee4\u63d0\u793a\u7b26\uff08\u9002\u7528\u4e8eWindows\uff09\u3002<\/p>\n<p><strong>\u5b89\u88c5Beautiful Soup 4\u540e\uff0c\u6211\u9700\u8981\u505a\u4ec0\u4e48\u624d\u80fd\u5f00\u59cb\u4f7f\u7528\u5b83\uff1f<\/strong><br \/>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165bs4\u6a21\u5757\u3002\u53ea\u9700\u5728\u4ee3\u7801\u6587\u4ef6\u7684\u5f00\u5934\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">from bs4 import BeautifulSoup\n<\/code><\/pre>\n<p>\u63a5\u4e0b\u6765\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Beautiful Soup\u6765\u89e3\u6790HTML\u6216XML\u6587\u6863\uff0c\u8fdb\u884c\u6570\u636e\u63d0\u53d6\u548c\u4fe1\u606f\u5904\u7406\u3002\u53ef\u4ee5\u67e5\u770b\u5b98\u65b9\u6587\u6863\u6216\u76f8\u5173\u6559\u7a0b\uff0c\u83b7\u53d6\u66f4\u591a\u4f7f\u7528\u793a\u4f8b\u548c\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n<p><strong>\u5982\u679c\u5728\u5b89\u88c5bs4\u65f6\u9047\u5230\u95ee\u9898\uff0c\u6211\u8be5\u5982\u4f55\u89e3\u51b3\uff1f<\/strong><br \/>\u5982\u679c\u5728\u5b89\u88c5\u8fc7\u7a0b\u4e2d\u9047\u5230\u95ee\u9898\uff0c\u5efa\u8bae\u68c0\u67e5\u4ee5\u4e0b\u51e0\u70b9\uff1a\u786e\u4fdd\u4f60\u7684pip\u7248\u672c\u662f\u6700\u65b0\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4<code>pip install --upgrade pip<\/code>\u8fdb\u884c\u5347\u7ea7\uff1b\u786e\u8ba4\u7f51\u7edc\u8fde\u63a5\u6b63\u5e38\uff0cpip\u9700\u8981\u8fde\u63a5\u5230\u4e92\u8054\u7f51\u4e0b\u8f7d\u5305\uff1b\u5982\u679c\u4f7f\u7528\u7684\u662f\u865a\u62df\u73af\u5883\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u6fc0\u6d3b\u5b83\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u4f7f\u7528<code>pip install beautifulsoup4 --user<\/code>\u547d\u4ee4\u6765\u8fdb\u884c\u7528\u6237\u7ea7\u522b\u7684\u5b89\u88c5\uff0c\u8fd9\u6709\u65f6\u53ef\u4ee5\u89e3\u51b3\u6743\u9650\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5b89\u88c5 BeautifulSoup4 (bs4) \u7684\u4e3b\u8981\u6b65\u9aa4\u6709\uff1a\u786e\u4fdd\u5df2\u5b89\u88c5 pip\u3001\u4f7f\u7528 pip \u5b89\u88c5 bs4\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1048256,"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\/1048251"}],"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=1048251"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1048251\/revisions"}],"predecessor-version":[{"id":1048257,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1048251\/revisions\/1048257"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1048256"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1048251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1048251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1048251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}