{"id":204021,"date":"2025-05-29T13:23:21","date_gmt":"2025-05-29T05:23:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204021\/"},"modified":"2025-05-29T13:23:21","modified_gmt":"2025-05-29T05:23:21","slug":"%e4%bd%bf%e7%94%a8-python-%e6%8a%93%e5%8f%96%e5%a4%9a%e4%b8%aa%e5%9b%be%e5%83%8f%e7%9a%84%e6%ad%a5%e9%aa%a4","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204021\/","title":{"rendered":"\u4f7f\u7528 Python \u6293\u53d6\u591a\u4e2a\u56fe\u50cf\u7684\u6b65\u9aa4"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u4f7f\u7528 Python \u6293\u53d6\u591a\u4e2a\u56fe\u50cf\u7684\u6b65\u9aa4<\/h1>\n<p>\u5bf9\u4e8e\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0c\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300a\u4f7f\u7528 Python \u6293\u53d6\u591a\u4e2a\u56fe\u50cf\u7684\u6b65\u9aa4\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01<\/p>\n<p><strong>\u7b2c 1 \u6b65\uff1a<\/strong> \u5b89\u88c5\u6240\u9700\u7684 Python \u5e93<\/p>\n<p>pip install beautifulsoup4 <br \/> pip \u5b89\u88c5\u8bf7\u6c42<br \/> pip installpillow # \u7528\u4e8e\u4fdd\u5b58\u56fe\u50cf<\/p>\n<p><strong>\u7b2c 2 \u6b65\uff1a<\/strong> \u5411\u7f51\u7ad9 URL \u53d1\u51fa GET \u8bf7\u6c42<\/p>\n<p>\u5bfc\u5165\u8bf7\u6c42<br \/> url = &#8220;&#8230;&#8221;<br \/> \u54cd\u5e94 = requests.get(url)<\/p>\n<p><strong>\u7b2c 3 \u6b65\uff1a\u7528 Beautiful Soup \u89e3\u6790 HTML<\/strong><\/p>\n<p>\u4ece bs4 \u5bfc\u5165 BeautifulSoup<br \/> soup = BeautifulSoup(response.text, &#8220;html.parser&#8221;)<\/p>\n<p><strong>\u7b2c\u56db\u6b65\uff1a\u67e5\u627e\u9875\u9762\u4e0a\u7684\u6240\u6709\u6807\u7b7e<\/strong><\/p>\n<p>\u56fe\u50cf = soup.find_all(&#8220;img&#8221;)<\/p>\n<p><strong>\u7b2c 5 \u6b65\uff1a\u5faa\u73af\u6bcf\u4e2a\u6807\u7b7e\u5e76\u4ece &#8216;src&#8217; \u5c5e\u6027\u4e2d\u63d0\u53d6\u56fe\u50cf URL<\/strong><\/p>\n<p>\u56fe\u50cf\u4e2d\u7684\u56fe\u50cf\uff1a<br \/> img_url = \u56fe\u7247[&#8216;src&#8217;]<\/p>\n<p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<ul>\n<li>\u5b8c\u5168\u63a7\u5236\u548c\u81ea\u5b9a\u4e49\u9009\u9879<\/li>\n<li>\u7075\u6d3b\u5730\u4e3a\u4e0d\u540c\u7f51\u7ad9\u5b9a\u5236\u811a\u672c<\/li>\n<\/ul>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<ul>\n<li>\u9700\u8981 Python \u7f16\u7a0b\u77e5\u8bc6<\/li>\n<li>\u6bd4\u89c6\u89c9\u5de5\u5177\u4e0d\u592a\u53cb\u597d<\/li>\n<\/ul>\n<p>\u5bf9\u4e8e\u6ca1\u6709\u7f16\u7a0b\u77e5\u8bc6\u7684\u7528\u6237\u6765\u8bf4\uff0cOctoparse \u662f\u4e00\u4e2a\u53ef\u89c6\u5316\u7f51\u7edc\u6293\u53d6\u5de5\u5177\uff0c\u53ef\u7528\u4e8e\u901a\u8fc7\u7b80\u5355\u7684\u62d6\u653e\u8fc7\u7a0b\u4ece\u7f51\u7ad9\u6293\u53d6\u591a\u4e2a\u56fe\u50cf\u3002 <\/p>\n<p>Octoparse \u63d0\u4f9b\u4e86 100 \u591a\u4e2a\u6613\u4e8e\u4f7f\u7528\u7684\u6a21\u677f\u6765\u63d0\u53d6\u6570\u636e\u3002\u8d85\u8fc7 30,000 \u540d\u7528\u6237\u4f7f\u7528\u8fd9\u4e9b\u6a21\u677f\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241019\/1729321264671359301bae0.jpg\" class=\"aligncenter\"><\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 Python \u6293\u53d6\u591a\u4e2a\u56fe\u50cf&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204021","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204021","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204021"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204021\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204021"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204021"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204021"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}