{"id":1143673,"date":"2025-01-08T22:52:30","date_gmt":"2025-01-08T14:52:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1143673.html"},"modified":"2025-01-08T22:52:33","modified_gmt":"2025-01-08T14:52:33","slug":"python2-7%e7%89%88%e6%9c%ac%e4%b8%ad%e5%a6%82%e4%bd%95%e5%ae%89%e8%a3%85bs4-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1143673.html","title":{"rendered":"python2.7\u7248\u672c\u4e2d\u5982\u4f55\u5b89\u88c5bs4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181150\/e9e59d03-b40d-4bd6-b76b-50910ea9c794.webp\" alt=\"python2.7\u7248\u672c\u4e2d\u5982\u4f55\u5b89\u88c5bs4\" \/><\/p>\n<p><p> <strong>\u5728Python 2.7\u7248\u672c\u4e2d\u5b89\u88c5BeautifulSoup 4\uff08bs4\uff09\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528pip\u547d\u4ee4\u3001\u4e0b\u8f7d\u6e90\u4ee3\u7801\u5e76\u624b\u52a8\u5b89\u88c5\u3001\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u7684\u5305\u7ba1\u7406\u5668\u3002\u8fd9\u4e09\u79cd\u65b9\u6cd5\u90fd\u53ef\u4ee5\u5e2e\u52a9\u4f60\u987a\u5229\u5b89\u88c5bs4\u6a21\u5757\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528pip\u547d\u4ee4<\/strong>\uff1a\u8fd9\u662f\u6700\u5e38\u89c1\u4e14\u6700\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u4e14\u80fd\u81ea\u52a8\u89e3\u51b3\u4f9d\u8d56\u5173\u7cfb\u3002\u4f60\u53ea\u9700\u8981\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165<code>pip install beautifulsoup4<\/code>\u3002\u7531\u4e8epip\u662fPython\u7684\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u5b83\u4f1a\u81ea\u52a8\u4ecePython\u5305\u7d22\u5f15\uff08PyPI\uff09\u4e0b\u8f7d\u5e76\u5b89\u88c5bs4\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e09\u79cd\u65b9\u6cd5\uff0c\u5e76\u4e14\u8ba8\u8bba\u4e00\u4e9b\u53ef\u80fd\u9047\u5230\u7684\u95ee\u9898\u53ca\u5176\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4f7f\u7528pip\u547d\u4ee4<\/strong><\/h2>\n<p><p>\u4f7f\u7528pip\u547d\u4ee4\u662f\u5b89\u88c5Python\u5305\u7684\u6700\u7b80\u5355\u548c\u6700\u65b9\u4fbf\u7684\u65b9\u6cd5\u3002pip\u662fPython\u7684\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u53ef\u4ee5\u81ea\u52a8\u4ecePython\u5305\u7d22\u5f15\uff08PyPI\uff09\u4e0b\u8f7d\u5e76\u5b89\u88c5\u6240\u9700\u7684\u5305\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528pip\u5b89\u88c5BeautifulSoup 4\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h2>\u5b89\u88c5pip<\/h2>\n<\/p>\n<p><p>\u867d\u7136Python 2.7\u81ea\u5e26\u4e86pip\uff0c\u4f46\u6709\u4e9b\u65e7\u7248\u672c\u53ef\u80fd\u6ca1\u6709\u5b89\u88c5\u6216\u9700\u8981\u66f4\u65b0\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\u6216\u66f4\u65b0pip\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">wget https:\/\/bootstrap.pypa.io\/pip\/2.7\/get-pip.py<\/p>\n<p>python get-pip.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4f7f\u7528pip\u5b89\u88c5bs4<\/h2>\n<\/p>\n<p><p>\u4e00\u65e6pip\u5b89\u88c5\u6216\u66f4\u65b0\u5b8c\u6210\uff0c\u4f60\u5c31\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u6765\u5b89\u88c5BeautifulSoup 4\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\u5c06\u4ecePython\u5305\u7d22\u5f15\uff08PyPI\uff09\u4e0b\u8f7d\u5e76\u5b89\u88c5\u6700\u65b0\u7248\u672c\u7684BeautifulSoup 4\u3002<\/p>\n<\/p>\n<p><h2>\u9a8c\u8bc1\u5b89\u88c5<\/h2>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u6765\u9a8c\u8bc1BeautifulSoup 4\u662f\u5426\u5b89\u88c5\u6210\u529f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import bs4<\/p>\n<p>print(bs4.__version__)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u6ca1\u6709\u62a5\u9519\u5e76\u4e14\u8f93\u51fa\u4e86\u7248\u672c\u53f7\uff0c\u90a3\u5c31\u8bf4\u660e\u5b89\u88c5\u6210\u529f\u4e86\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4e0b\u8f7d\u6e90\u4ee3\u7801\u5e76\u624b\u52a8\u5b89\u88c5<\/strong><\/h2>\n<p><p>\u6709\u65f6\u5019\uff0c\u7531\u4e8e\u7f51\u7edc\u6216\u5176\u4ed6\u539f\u56e0\uff0c\u4f7f\u7528pip\u53ef\u80fd\u4f1a\u9047\u5230\u95ee\u9898\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u4ee5\u9009\u62e9\u624b\u52a8\u4e0b\u8f7dBeautifulSoup 4\u7684\u6e90\u4ee3\u7801\u5e76\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><h2>\u4e0b\u8f7d\u6e90\u4ee3\u7801<\/h2>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4eceBeautifulSoup 4\u7684\u5b98\u65b9GitHub\u9875\u9762\u6216Python\u5305\u7d22\u5f15\uff08PyPI\uff09\u4e0b\u8f7d\u6e90\u4ee3\u7801\u3002\u4ee5\u4e0b\u662f\u4ecePyPI\u4e0b\u8f7d\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00<a href=\"https:\/\/pypi.org\/project\/beautifulsoup4\/\">BeautifulSoup 4\u7684PyPI\u9875\u9762<\/a>.<\/li>\n<li>\u4e0b\u8f7d\u6700\u65b0\u7248\u672c\u7684\u6e90\u4ee3\u7801\u538b\u7f29\u5305\uff08\u901a\u5e38\u662f\u4e00\u4e2a<code>.tar.gz<\/code>\u6587\u4ef6\uff09\u3002<\/li>\n<\/ol>\n<p><h2>\u89e3\u538b\u5e76\u5b89\u88c5<\/h2>\n<\/p>\n<p><p>\u4e0b\u8f7d\u5b8c\u6210\u540e\uff0c\u89e3\u538b\u7f29\u6587\u4ef6\u5e76\u8fdb\u5165\u89e3\u538b\u540e\u7684\u76ee\u5f55\u3002\u7136\u540e\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">tar -xzf beautifulsoup4-&lt;version&gt;.tar.gz<\/p>\n<p>cd beautifulsoup4-&lt;version&gt;<\/p>\n<p>python setup.py install<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u624b\u52a8\u5b89\u88c5BeautifulSoup 4\u5230\u4f60\u7684Python\u73af\u5883\u4e2d\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u7684\u5305\u7ba1\u7406\u5668<\/strong><\/h2>\n<p><p>\u4e00\u4e9b\u64cd\u4f5c\u7cfb\u7edf\u81ea\u5e26\u7684\u5305\u7ba1\u7406\u5668\u4e5f\u53ef\u4ee5\u5b89\u88c5BeautifulSoup 4\u3002\u8fd9\u79cd\u65b9\u6cd5\u901a\u5e38\u6bd4\u8f83\u7b80\u5355\uff0c\u56e0\u4e3a\u5305\u7ba1\u7406\u5668\u4f1a\u81ea\u52a8\u89e3\u51b3\u4f9d\u8d56\u5173\u7cfb\u3002<\/p>\n<\/p>\n<p><h2>\u5728Debian\/Ubuntu\u4e0a<\/h2>\n<\/p>\n<p><p>\u5728Debian\u6216Ubuntu\u7cfb\u7edf\u4e0a\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>apt-get<\/code>\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt-get update<\/p>\n<p>sudo apt-get install python-bs4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u5728CentOS\/Fedora\u4e0a<\/h2>\n<\/p>\n<p><p>\u5728CentOS\u6216Fedora\u7cfb\u7edf\u4e0a\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>yum<\/code>\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo yum install python-beautifulsoup4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e9b\u547d\u4ee4\u4f1a\u4ece\u64cd\u4f5c\u7cfb\u7edf\u7684\u5305\u4ed3\u5e93\u4e2d\u4e0b\u8f7d\u5e76\u5b89\u88c5BeautifulSoup 4\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6cd5<\/strong><\/h2>\n<p><h2>\u7f51\u7edc\u95ee\u9898<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u5728\u4f7f\u7528pip\u5b89\u88c5\u65f6\u9047\u5230\u4e86\u7f51\u7edc\u95ee\u9898\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u4f7f\u7528\u56fd\u5185\u7684\u955c\u50cf\u6e90\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u963f\u91cc\u4e91\u7684\u955c\u50cf\u6e90\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install beautifulsoup4 -i https:\/\/mirrors.aliyun.com\/pypi\/simple\/<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4f9d\u8d56\u95ee\u9898<\/h2>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0cBeautifulSoup 4\u53ef\u80fd\u4f1a\u4f9d\u8d56\u4e8e\u5176\u4ed6Python\u5305\uff0c\u5982<code>lxml<\/code>\u6216<code>html5lib<\/code>\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7pip\u5b89\u88c5\u8fd9\u4e9b\u4f9d\u8d56\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install lxml html5lib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u73af\u5883\u53d8\u91cf\u95ee\u9898<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u5728\u5b89\u88c5\u540e\u4ecd\u7136\u65e0\u6cd5\u5bfc\u5165bs4\uff0c\u53ef\u80fd\u662f\u56e0\u4e3a\u73af\u5883\u53d8\u91cf\u6ca1\u6709\u6b63\u786e\u8bbe\u7f6e\u3002\u786e\u4fdd\u4f60\u7684Python\u8def\u5f84\u5728\u7cfb\u7edf\u7684\u73af\u5883\u53d8\u91cf\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">export PYTHONPATH=$PYTHONPATH:\/path\/to\/python\/site-packages<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e94\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u5728Python 2.7\u7248\u672c\u4e2d\u5b89\u88c5BeautifulSoup 4\uff08bs4\uff09\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528pip\u547d\u4ee4\u3001\u4e0b\u8f7d\u6e90\u4ee3\u7801\u5e76\u624b\u52a8\u5b89\u88c5\u3001\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u7684\u5305\u7ba1\u7406\u5668\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u4f46\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528pip\u547d\u4ee4\u662f\u6700\u7b80\u5355\u548c\u6700\u65b9\u4fbf\u7684\u3002\u901a\u8fc7\u9075\u5faa\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u4f60\u5e94\u8be5\u80fd\u591f\u987a\u5229\u5b89\u88c5\u5e76\u4f7f\u7528BeautifulSoup 4\u8fdb\u884c\u7f51\u9875\u89e3\u6790\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python 2.7\u4e2d\u5b89\u88c5Beautiful Soup 4\uff08bs4\uff09\uff1f<\/strong><br \/>\u5728Python 2.7\u73af\u5883\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u6765\u5b89\u88c5Beautiful Soup 4\u3002\u786e\u4fdd\u60a8\u7684pip\u662f\u6700\u65b0\u7248\u672c\uff0c\u60a8\u53ef\u4ee5\u8fd0\u884c<code>pip install --upgrade pip<\/code>\u6765\u66f4\u65b0\u3002\u63a5\u7740\uff0c\u8f93\u5165<code>pip install beautifulsoup4<\/code>\u6765\u5b8c\u6210\u5b89\u88c5\u3002\u5b89\u88c5\u6210\u529f\u540e\uff0c\u60a8\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165bs4\u4ee5\u5f00\u59cb\u4f7f\u7528\u3002<\/p>\n<p><strong>Beautiful Soup 4\u4e0e\u5176\u4ed6\u7248\u672c\u7684Python\u517c\u5bb9\u5417\uff1f<\/strong><br \/>Beautiful Soup 4\u4e3b\u8981\u652f\u6301Python 2.7\u548cPython 3.x\u7248\u672c\u3002\u867d\u7136\u5efa\u8bae\u4f7f\u7528Python 3.x\u7248\u672c\u4ee5\u83b7\u5f97\u66f4\u597d\u7684\u6027\u80fd\u548c\u5b89\u5168\u6027\uff0c\u4f46bs4\u5728Python 2.7\u4e2d\u4ecd\u7136\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u3002\u5982\u679c\u60a8\u9700\u8981\u5728\u65b0\u9879\u76ee\u4e2d\u4f7f\u7528bs4\uff0c\u8003\u8651\u5347\u7ea7\u5230Python 3.x\u4ee5\u4fbf\u4e8e\u540e\u7eed\u7ef4\u62a4\u3002<\/p>\n<p><strong>\u5728Python 2.7\u4e2d\u5b89\u88c5bs4\u540e\uff0c\u5982\u4f55\u786e\u8ba4\u5b89\u88c5\u6210\u529f\uff1f<\/strong><br \/>\u5b89\u88c5\u5b8cBeautiful Soup 4\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728Python\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u4ee3\u7801\u6765\u786e\u8ba4\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\u4fe1\u606f\uff0c\u5e76\u4e14\u8f93\u51fa\u4e86\u4e00\u4e2aBeautifulSoup\u5bf9\u8c61\uff0c\u5219\u8bf4\u660e\u5b89\u88c5\u6210\u529f\u3002<\/p>\n<p><strong>\u5728Python 2.7\u4e2d\u4f7f\u7528bs4\u65f6\uff0c\u5982\u4f55\u5904\u7406\u5e38\u89c1\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Beautiful Soup 4\u65f6\uff0c\u5e38\u89c1\u9519\u8bef\u5305\u62ec\u6a21\u5757\u672a\u627e\u5230\u6216\u89e3\u6790\u9519\u8bef\u3002\u786e\u4fdd\u60a8\u5df2\u6b63\u786e\u5b89\u88c5bs4\uff0c\u4f7f\u7528<code>pip list<\/code>\u68c0\u67e5\u5df2\u5b89\u88c5\u7684\u5305\u3002\u5982\u679c\u9047\u5230\u89e3\u6790HTML\u7684\u95ee\u9898\uff0c\u786e\u4fdd\u60a8\u4f20\u5165\u7684HTML\u4ee3\u7801\u662f\u6709\u6548\u7684\uff0c\u6216\u8005\u5c1d\u8bd5\u4f7f\u7528\u5176\u4ed6\u89e3\u6790\u5668\uff0c\u5982<code>lxml<\/code>\u6216<code>html5lib<\/code>\u3002\u53ef\u4ee5\u901a\u8fc7<code>pip install lxml<\/code>\u548c<code>pip install html5lib<\/code>\u6765\u5b89\u88c5\u8fd9\u4e9b\u89e3\u6790\u5668\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python 2.7\u7248\u672c\u4e2d\u5b89\u88c5BeautifulSoup 4\uff08bs4\uff09\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528pip\u547d\u4ee4\u3001\u4e0b\u8f7d\u6e90\u4ee3\u7801 [&hellip;]","protected":false},"author":3,"featured_media":1143684,"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\/1143673"}],"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=1143673"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1143673\/revisions"}],"predecessor-version":[{"id":1143687,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1143673\/revisions\/1143687"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1143684"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1143673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1143673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1143673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}