{"id":951,"date":"2025-08-08T22:31:40","date_gmt":"2025-08-08T14:31:40","guid":{"rendered":"https:\/\/www.vcidc.net\/blog\/?p=951"},"modified":"2025-08-08T22:31:40","modified_gmt":"2025-08-08T14:31:40","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8python%e5%92%8cbeautifulsoup%e4%bb%8e%e7%bd%91%e7%ab%99%e8%8e%b7%e5%8f%96%e6%95%b0%e6%8d%ae%ef%bc%9a%e5%be%b7%e8%ae%af%e7%94%b5%e8%ae%af%e6%8e%92%e5%90%8d%e7%a4%ba","status":"publish","type":"post","link":"https:\/\/www.vcidc.net\/blog\/951.html","title":{"rendered":"\u5982\u4f55\u4f7f\u7528Python\u548cBeautifulSoup\u4ece\u7f51\u7ad9\u83b7\u53d6\u6570\u636e\uff1a\u5fb7\u8baf\u7535\u8baf\u6392\u540d\u793a\u4f8b"},"content":{"rendered":"<p><p>\u672c\u6587\u5c06\u6307\u5bfc\u60a8\u5982\u4f55\u4f7f\u7528Python\u548cBeautifulSoup\u5e93\u4ece\u7f51\u7ad9\u6293\u53d6\u6570\u636e\u3002\u6211\u4eec\u5c06\u901a\u8fc7\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\u6765\u5c55\u793a\u5982\u4f55\u5b8c\u6210\u8fd9\u9879\u4efb\u52a1\uff0c\u5305\u62ec\u64cd\u4f5c\u524d\u7684\u51c6\u5907\u3001\u8be6\u7ec6\u64cd\u4f5c\u6b65\u9aa4\u3001\u5173\u952e\u547d\u4ee4\u548c\u4ee3\u7801\u793a\u4f8b\uff0c\u4ee5\u53ca\u53ef\u80fd\u9047\u5230\u7684\u95ee\u9898\u548c\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.vcidc.net\/blog\/wp-content\/uploads\/2025\/07\/k9ItXv.jpg\" alt=\"\u5982\u4f55\u4f7f\u7528Python\u548cBeautifulSoup\u4ece\u7f51\u7ad9\u83b7\u53d6\u6570\u636e\uff1a\u5fb7\u8baf\u7535\u8baf\u6392\u540d\u793a\u4f8b\" title=\"\u5982\u4f55\u4f7f\u7528Python\u548cBeautifulSoup\u4ece\u7f51\u7ad9\u83b7\u53d6\u6570\u636e\uff1a\u5fb7\u8baf\u7535\u8baf\u6392\u540d\u793a\u4f8b\"><\/p>\n<\/p>\n<p><h2>\u64cd\u4f5c\u524d\u7684\u51c6\u5907<\/h2>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u8bf7\u786e\u4fdd\u60a8\u7684\u7535\u8111\u4e0a\u5df2\u5b89\u88c5\u4ee5\u4e0b\u8f6f\u4ef6\u548c\u5e93\uff1a<\/p>\n<\/p>\n<ul>\n<li>Python 3.x<\/li>\n<li>pip\uff08Python\u5305\u7ba1\u7406\u5668\uff09<\/li>\n<li>BeautifulSoup 4.x<\/li>\n<\/ul>\n<p><p>\u5982\u679c\u60a8\u8fd8\u6ca1\u6709\u5b89\u88c5\u8fd9\u4e9b\uff0c\u8bf7\u5148\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code>pip install beautifulsoup4<\/code><\/pre>\n<\/p>\n<p><h2>\u4efb\u52a1\uff1a\u4ece\u7f51\u7ad9\u6293\u53d6\u6570\u636e<\/h2>\n<\/p>\n<p><p>\u6211\u4eec\u5c06\u4ece\u67d0\u4e2a\u65b0\u95fb\u7f51\u7ad9\u6293\u53d6\u6700\u65b0\u7684\u65b0\u95fb\u6807\u9898\u548c\u6458\u8981\u3002\u5047\u8bbe\u7f51\u7ad9\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>https:\/\/example.com\/news<\/p>\r\n<p>  -&gt; &lt;div class=\"news-list\"&gt;<\/p>\r\n<p>        -&gt; &lt;div class=\"news-item\"&gt;<\/p>\r\n<p>                -&gt; &lt;h3 class=\"news-title\"&gt;&lt;\/h3&gt;<\/p>\r\n<p>                -&gt; &lt;p class=\"news-summary\"&gt;&lt;\/p&gt;<\/p>\r\n<p>        -&gt; &lt;div class=\"news-item\"&gt;<\/p>\r\n<p>                -&gt; &lt;h3 class=\"news-title\"&gt;&lt;\/h3&gt;<\/p>\r\n<p>                -&gt; &lt;p class=\"news-summary\"&gt;&lt;\/p&gt;<\/p>\r\n<p>        -&gt; ...<\/p>\r\n<p>    &lt;\/div&gt;<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6b65\u9aa4 1\uff1a\u5bfc\u5165\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code>from bs4 import BeautifulSoup<\/p>\r\n<p>import requests<\/code><\/pre>\n<\/p>\n<p><h3>\u6b65\u9aa4 2\uff1a\u53d1\u9001HTTP\u8bf7\u6c42<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528requests\u5e93\u53d1\u9001HTTP GET\u8bf7\u6c42\u5230\u76ee\u6807\u7f51\u7ad9\u3002<\/p>\n<\/p>\n<p><pre><code>url = 'https:\/\/example.com\/news'<\/p>\r\n<p>response = requests.get(url)<\/code><\/pre>\n<\/p>\n<p><p>\u786e\u4fdd\u68c0\u67e5\u54cd\u5e94\u72b6\u6001\u7801\uff0c\u786e\u8ba4\u8bf7\u6c42\u6210\u529f\u3002<\/p>\n<\/p>\n<p><pre><code>if response.status_code == 200:<\/p>\r\n<p>     \u8bf7\u6c42\u6210\u529f\uff0c\u7ee7\u7eed\u5904\u7406\u6570\u636e<\/p>\r\n<p>    pass<\/p>\r\n<p>else:<\/p>\r\n<p>     \u8bf7\u6c42\u5931\u8d25\uff0c\u5904\u7406\u9519\u8bef<\/p>\r\n<p>    pass<\/code><\/pre>\n<\/p>\n<p><h3>\u6b65\u9aa4 3\uff1a\u89e3\u6790HTML\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code>soup = BeautifulSoup(response.text, 'html.parser')<\/code><\/pre>\n<\/p>\n<p><h3>\u6b65\u9aa4 4\uff1a\u63d0\u53d6\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u627e\u5230\u5305\u542b\u65b0\u95fb\u9879\u7684\u5bb9\u5668\uff0c\u5e76\u904d\u5386\u6bcf\u4e2a\u65b0\u95fb\u9879\u3002<\/p>\n<\/p>\n<p><pre><code>news_list = soup.find('div', class_='news-list')<\/p>\r\n<p>for news_item in news_list.find_all('div', class_='news-item'):<\/p>\r\n<p>    title = news_item.find('h3', class_='news-title').text<\/p>\r\n<p>    summary = news_item.find('p', class_='news-summary').text<\/p>\r\n<p>    print(f'Title: {title}\\nSummary: {summary}\\n')<\/code><\/pre>\n<\/p>\n<p><h2>\u5173\u952e\u547d\u4ee4\u548c\u4ee3\u7801\u89e3\u91ca<\/h2>\n<\/p>\n<p><p><strong>requests.get(url)<\/strong>\uff1a\u53d1\u9001HTTP GET\u8bf7\u6c42\u5230\u6307\u5b9a\u7684URL\u3002<\/p>\n<\/p>\n<p><p><strong>BeautifulSoup(html_content, parser)<\/strong>\uff1a\u521b\u5efa\u4e00\u4e2aBeautifulSoup\u5bf9\u8c61\uff0c\u7528\u4e8e\u89e3\u6790HTML\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p><strong>find(tag, attributes)<\/strong>\uff1a\u67e5\u627e\u7b2c\u4e00\u4e2a\u5339\u914d\u6307\u5b9a\u6807\u7b7e\u548c\u5c5e\u6027\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p><strong>find_all(tag, attributes)<\/strong>\uff1a\u67e5\u627e\u6240\u6709\u5339\u914d\u6307\u5b9a\u6807\u7b7e\u548c\u5c5e\u6027\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p><strong>text<\/strong>\uff1a\u83b7\u53d6\u5143\u7d20\u7684\u6587\u672c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h2>\u53ef\u80fd\u9047\u5230\u7684\u95ee\u9898\u548c\u6ce8\u610f\u4e8b\u9879<\/h2>\n<\/p>\n<ul>\n<li>\u7f51\u7ad9\u53ef\u80fd\u6709\u53cd\u722c\u866b\u673a\u5236\uff0c\u9700\u8981\u8bbe\u7f6e\u5408\u9002\u7684\u8bf7\u6c42\u5934\uff08\u5982User-Agent\uff09\u3002<\/li>\n<li>\u7f51\u7ad9\u7ed3\u6784\u53ef\u80fd\u53d1\u751f\u53d8\u5316\uff0c\u9700\u8981\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u8c03\u6574\u9009\u62e9\u5668\u3002<\/li>\n<li>\u8bf7\u6c42\u9891\u7387\u8fc7\u9ad8\u53ef\u80fd\u5bfc\u81f4IP\u88ab\u5c01\u7981\uff0c\u6ce8\u610f\u5408\u7406\u63a7\u5236\u8bf7\u6c42\u95f4\u9694\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u5c06\u6307\u5bfc\u60a8\u5982\u4f55\u4f7f\u7528Python\u548cBeautifulSoup\u5e93\u4ece\u7f51\u7ad9\u6293\u53d6\u6570\u636e\u3002\u6211\u4eec\u5c06\u901a\u8fc7\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\u6765\u5c55\u793a\u5982\u4f55\u5b8c\u6210\u8fd9\u9879\u4efb\u52a1\uff0c\u5305\u62ec\u64cd\u4f5c\u524d\u7684\u51c6\u5907\u3001\u8be6\u7ec6\u64cd\u4f5c\u6b65\u9aa4\u3001\u5173\u952e\u547d\u4ee4\u548c\u4ee3\u7801\u793a\u4f8b\uff0c\u4ee5\u53ca\u53ef\u80fd\u9047\u5230\u7684\u95ee\u9898\u548c\u6ce8\u610f\u4e8b\u9879\u3002 \u64cd\u4f5c\u524d\u7684\u51c6\u5907 \u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u8bf7\u786e&#8230;<\/p>\n","protected":false},"author":1,"featured_media":952,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[376,375,377],"topic":[],"class_list":["post-951","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-usa-server","tag-beautifulsoup","tag-python","tag-377"],"_links":{"self":[{"href":"https:\/\/www.vcidc.net\/blog\/wp-json\/wp\/v2\/posts\/951","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vcidc.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.vcidc.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.vcidc.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vcidc.net\/blog\/wp-json\/wp\/v2\/comments?post=951"}],"version-history":[{"count":1,"href":"https:\/\/www.vcidc.net\/blog\/wp-json\/wp\/v2\/posts\/951\/revisions"}],"predecessor-version":[{"id":954,"href":"https:\/\/www.vcidc.net\/blog\/wp-json\/wp\/v2\/posts\/951\/revisions\/954"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.vcidc.net\/blog\/wp-json\/wp\/v2\/media\/952"}],"wp:attachment":[{"href":"https:\/\/www.vcidc.net\/blog\/wp-json\/wp\/v2\/media?parent=951"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vcidc.net\/blog\/wp-json\/wp\/v2\/categories?post=951"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vcidc.net\/blog\/wp-json\/wp\/v2\/tags?post=951"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.vcidc.net\/blog\/wp-json\/wp\/v2\/topic?post=951"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}