{"id":1026584,"date":"2024-12-31T10:43:53","date_gmt":"2024-12-31T02:43:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1026584.html"},"modified":"2024-12-31T10:43:55","modified_gmt":"2024-12-31T02:43:55","slug":"%e5%8f%aa%e4%b8%8b%e4%ba%86python%e5%a6%82%e4%bd%95%e5%ae%89%e8%a3%85bs4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1026584.html","title":{"rendered":"\u53ea\u4e0b\u4e86python\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\/956e231b-db5b-408d-9685-4ea79b703744.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u53ea\u4e0b\u4e86python\u5982\u4f55\u5b89\u88c5bs4\" \/><\/p>\n<p><p> <strong>\u8981\u5b89\u88c5bs4\uff08BeautifulSoup 4\uff09\uff0c\u53ea\u9700\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u4e00\u6761\u7b80\u5355\u7684\u547d\u4ee4\uff1a<code>pip install beautifulsoup4<\/code>\u3002<\/strong> \u8fd9\u6761\u547d\u4ee4\u4f1a\u901a\u8fc7Python\u7684\u5305\u7ba1\u7406\u5668pip\u4e0b\u8f7d\u5e76\u5b89\u88c5BeautifulSoup 4\u5e93\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u5c31\u53ef\u4ee5\u5728Python\u4ee3\u7801\u4e2d\u5bfc\u5165\u548c\u4f7f\u7528BeautifulSoup 4\u4e86\u3002\u8be6\u7ec6\u7684\u5b89\u88c5\u6b65\u9aa4\u5305\u62ec\uff1a\u6253\u5f00\u7ec8\u7aef\u3001\u786e\u4fdd\u5df2\u5b89\u88c5pip\u3001\u6267\u884c\u5b89\u88c5\u547d\u4ee4\u3001\u9a8c\u8bc1\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u786e\u4fdd\u5df2\u5b89\u88c5pip<\/h2>\n<\/p>\n<p><p>pip\u662fPython\u7684\u5305\u7ba1\u7406\u5668\uff0c\u7528\u4e8e\u5b89\u88c5\u548c\u7ba1\u7406Python\u5305\u3002\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5pip\uff0c\u9700\u8981\u5148\u5b89\u88c5\u5b83\u3002\u5927\u591a\u6570\u73b0\u4ee3\u7684Python\u53d1\u884c\u7248\u90fd\u81ea\u5e26pip\uff0c\u4f46\u5982\u679c\u6ca1\u6709\uff0c\u53ef\u4ee5\u6309\u4ee5\u4e0b\u6b65\u9aa4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>Windows\u7cfb\u7edf<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u4e0b\u8f7d<a href=\"https:\/\/bootstrap.pypa.io\/get-pip.py\">get-pip.py<\/a>\u6587\u4ef6\u3002<\/li>\n<li>\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\uff1a<code>python get-pip.py<\/code>\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>MacOS\u548cLinux\u7cfb\u7edf<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u6253\u5f00\u7ec8\u7aef\u3002<\/li>\n<li>\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a\n<pre><code class=\"language-sh\">curl https:\/\/bootstrap.pypa.io\/get-pip.py -o get-pip.py<\/p>\n<p>python3 get-pip.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><h2>\u4e8c\u3001\u5b89\u88c5BeautifulSoup 4<\/h2>\n<\/p>\n<p><p>\u5b89\u88c5BeautifulSoup 4\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install beautifulsoup4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6761\u547d\u4ee4\u4f1a\u81ea\u52a8\u4ecePython\u5b98\u65b9\u5305\u4ed3\u5e93\uff08PyPI\uff09\u4e0b\u8f7d\u5e76\u5b89\u88c5BeautifulSoup 4\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u9a8c\u8bc1\u5b89\u88c5<\/h2>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u6765\u9a8c\u8bc1BeautifulSoup 4\u662f\u5426\u5df2\u6210\u529f\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00Python\u89e3\u91ca\u5668\uff08\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165<code>python<\/code>\u6216<code>python3<\/code>\uff09\u3002<\/li>\n<li>\u8f93\u5165\u4ee5\u4e0b\u4ee3\u7801\uff1a\n<pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>print(BeautifulSoup)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u5982\u679c\u6ca1\u6709\u51fa\u73b0\u9519\u8bef\u6d88\u606f\uff0c\u5e76\u4e14\u8f93\u51fa\u4e86BeautifulSoup\u7c7b\u7684\u4fe1\u606f\uff0c\u5219\u8868\u793a\u5b89\u88c5\u6210\u529f\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528BeautifulSoup 4\u8fdb\u884c\u7f51\u9875\u89e3\u6790<\/h2>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528BeautifulSoup 4\u6765\u89e3\u6790HTML\u6216XML\u6587\u6863\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u521b\u5efa\u4e00\u4e2aHTML\u6587\u4ef6\uff08\u4f8b\u5982<code>example.html<\/code>\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;html&gt;<\/p>\n<p>  &lt;head&gt;&lt;title&gt;Test Page&lt;\/title&gt;&lt;\/head&gt;<\/p>\n<p>  &lt;body&gt;<\/p>\n<p>    &lt;h1&gt;Hello, World!&lt;\/h1&gt;<\/p>\n<p>    &lt;p&gt;This is a test page.&lt;\/p&gt;<\/p>\n<p>  &lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528BeautifulSoup 4\u89e3\u6790\u8be5HTML\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<h2><strong>\u8bfb\u53d6HTML\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&quot;example.html&quot;, &quot;r&quot;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<h2><strong>\u521b\u5efaBeautifulSoup\u5bf9\u8c61<\/strong><\/h2>\n<p>soup = BeautifulSoup(content, &quot;html.parser&quot;)<\/p>\n<h2><strong>\u8f93\u51fa\u6807\u9898\u5185\u5bb9<\/strong><\/h2>\n<p>print(soup.title.string)<\/p>\n<h2><strong>\u8f93\u51fa\u6240\u6709\u6bb5\u843d\u5185\u5bb9<\/strong><\/h2>\n<p>for paragraph in soup.find_all(&quot;p&quot;):<\/p>\n<p>    print(paragraph.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u8bfb\u53d6\u4e86\u4e00\u4e2aHTML\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u7136\u540e\u4f7f\u7528BeautifulSoup 4\u89e3\u6790\u8be5\u5185\u5bb9\uff0c\u5e76\u8f93\u51fa\u4e86\u6807\u9898\u548c\u6bb5\u843d\u7684\u6587\u672c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>\u5728\u5b89\u88c5\u548c\u4f7f\u7528BeautifulSoup 4\u7684\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u7684\u95ee\u9898\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u89e3\u51b3\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>pip\u547d\u4ee4\u672a\u627e\u5230<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u786e\u4fddpip\u5df2\u6b63\u786e\u5b89\u88c5\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u91cd\u65b0\u5b89\u88c5pip\u3002<\/li>\n<li>\u68c0\u67e5\u73af\u5883\u53d8\u91cf\u662f\u5426\u6b63\u786e\u914d\u7f6e\uff0c\u786e\u4fddPython\u548cpip\u7684\u8def\u5f84\u5df2\u6dfb\u52a0\u5230\u7cfb\u7edf\u7684PATH\u4e2d\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>BeautifulSoup 4\u5b89\u88c5\u5931\u8d25<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u68c0\u67e5\u7f51\u7edc\u8fde\u63a5\u662f\u5426\u6b63\u5e38\uff0c\u786e\u4fdd\u53ef\u4ee5\u8bbf\u95eePyPI\u3002<\/li>\n<li>\u5c1d\u8bd5\u4f7f\u7528<code>pip install --upgrade pip<\/code>\u66f4\u65b0pip\u7248\u672c\uff0c\u7136\u540e\u91cd\u65b0\u5b89\u88c5BeautifulSoup 4\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5bfc\u5165BeautifulSoup 4\u65f6\u62a5\u9519<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u786e\u4fdd\u5df2\u6b63\u786e\u5b89\u88c5BeautifulSoup 4\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u91cd\u65b0\u5b89\u88c5\u3002<\/li>\n<li>\u68c0\u67e5Python\u73af\u5883\u662f\u5426\u6b63\u786e\uff0c\u786e\u4fdd\u5728\u6b63\u786e\u7684Python\u73af\u5883\u4e2d\u5b89\u88c5\u548c\u4f7f\u7528BeautifulSoup 4\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u5e94\u8be5\u53ef\u4ee5\u987a\u5229\u5b89\u88c5\u5e76\u4f7f\u7528BeautifulSoup 4\u6765\u89e3\u6790\u548c\u5904\u7406HTML\u6587\u6863\u3002\u5982\u679c\u5728\u5b9e\u9645\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u9047\u5230\u5176\u4ed6\u95ee\u9898\uff0c\u53ef\u4ee5\u67e5\u9605BeautifulSoup 4\u7684<a href=\"https:\/\/www.crummy.com\/software\/BeautifulSoup\/bs4\/doc\/\">\u5b98\u65b9\u6587\u6863<\/a>\u83b7\u53d6\u66f4\u591a\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b89\u88c5Beautiful Soup 4\uff08bs4\uff09\uff1f<\/strong><br \/>\u8981\u5b89\u88c5Beautiful Soup 4\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5305\u7ba1\u7406\u5de5\u5177pip\u3002\u5728\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a  <\/p>\n<pre><code class=\"language-bash\">pip install beautifulsoup4\n<\/code><\/pre>\n<p>\u786e\u4fdd\u60a8\u5df2\u7ecf\u6b63\u786e\u5b89\u88c5\u4e86Python\u548cpip\u3002\u5982\u679c\u60a8\u4f7f\u7528\u7684\u662fPython 3\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528<code>pip3<\/code>\u547d\u4ee4\u3002<\/p>\n<p><strong>\u5728\u5b89\u88c5bs4\u65f6\u662f\u5426\u9700\u8981\u5176\u4ed6\u4f9d\u8d56\uff1f<\/strong><br \/>Beautiful Soup 4\u901a\u5e38\u4e0d\u9700\u8981\u5176\u4ed6\u4f9d\u8d56\uff0c\u4f46\u5982\u679c\u60a8\u5e0c\u671b\u89e3\u6790HTML\u548cXML\u6587\u6863\uff0c\u5efa\u8bae\u540c\u65f6\u5b89\u88c5<code>lxml<\/code>\u6216<code>html5lib<\/code>\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u540c\u65f6\u5b89\u88c5\uff1a  <\/p>\n<pre><code class=\"language-bash\">pip install beautifulsoup4 lxml html5lib\n<\/code><\/pre>\n<p><strong>\u5982\u679c\u5728\u5b89\u88c5bs4\u65f6\u9047\u5230\u6743\u9650\u95ee\u9898\uff0c\u5e94\u8be5\u600e\u4e48\u505a\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u5728\u5b89\u88c5\u8fc7\u7a0b\u4e2d\u9047\u5230\u6743\u9650\u95ee\u9898\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u5728\u547d\u4ee4\u524d\u52a0\u4e0a<code>sudo<\/code>\uff08\u5bf9\u4e8eLinux\u548cMacOS\u7528\u6237\uff09\u6216\u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u8fd0\u884c\u547d\u4ee4\u63d0\u793a\u7b26\uff08Windows\u7528\u6237\uff09\u3002\u547d\u4ee4\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-bash\">sudo pip install beautifulsoup4\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u5141\u8bb8\u60a8\u83b7\u5f97\u5fc5\u8981\u7684\u6743\u9650\u6765\u5b8c\u6210\u5b89\u88c5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5b89\u88c5bs4\uff08BeautifulSoup 4\uff09\uff0c\u53ea\u9700\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u4e00\u6761\u7b80\u5355\u7684\u547d\u4ee4\uff1apip install [&hellip;]","protected":false},"author":3,"featured_media":1026595,"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\/1026584"}],"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=1026584"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1026584\/revisions"}],"predecessor-version":[{"id":1026599,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1026584\/revisions\/1026599"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1026595"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1026584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1026584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1026584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}