{"id":96982,"date":"2024-10-20T14:00:18","date_gmt":"2024-10-20T06:00:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/96982\/"},"modified":"2024-10-20T14:00:19","modified_gmt":"2024-10-20T06:00:19","slug":"python3%e7%8e%a9%e8%bd%89%e7%b6%b2%e7%b5%a1%ef%bc%9a%e7%8d%b2%e5%8f%96%e7%b6%b2%e9%a0%81%e6%95%b8%e6%93%9a%e5%ba%ab-python3-%e7%8d%b2%e5%8f%96%e7%b6%b2%e9%a0%81%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/96982\/","title":{"rendered":"Python3\u73a9\u8f49\u7db2\u7d61\uff1a\u7372\u53d6\u7db2\u9801\u6578\u64da\u5eab (python3 \u7372\u53d6\u7db2\u9801\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"python3%e7%8e%a9%e8%bd%89%e7%b6%b2%e7%b5%a1%ef%bc%9a%e7%8d%b2%e5%8f%96%e7%b6%b2%e9%a0%81%e6%95%b8%e6%93%9a%e5%ba%ab-OQuMWbNFNb\">Python3\u73a9\u8f49\u7db2\u7d61\uff1a\u7372\u53d6\u7db2\u9801\u6578\u64da\u5eab<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u6642\u4ee3\uff0c\u7db2\u9801\u6578\u64da\u7684\u7372\u53d6\u6210\u70ba\u4e86\u8a31\u591a\u958b\u767c\u8005\u548c\u6578\u64da\u79d1\u5b78\u5bb6\u7684\u91cd\u8981\u4efb\u52d9\u3002Python3\u4f5c\u70ba\u4e00\u7a2e\u5f37\u5927\u7684\u7de8\u7a0b\u8a9e\u8a00\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u5de5\u5177\u548c\u5eab\u4f86\u8f15\u9b06\u5730\u5f9e\u7db2\u9801\u4e2d\u63d0\u53d6\u6578\u64da\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Python3\u4f86\u7372\u53d6\u7db2\u9801\u6578\u64da\u5eab\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87python3%ef%bc%9f-OQuMWbNFNb\">\u70ba\u4ec0\u9ebc\u9078\u64c7Python3\uff1f<\/h2>\n<p>Python3\u56e0\u5176\u7c21\u6f54\u7684\u8a9e\u6cd5\u548c\u5f37\u5927\u7684\u5eab\u652f\u6301\u800c\u53d7\u5230\u5ee3\u6cdb\u6b61\u8fce\u3002\u7279\u5225\u662f\u5728\u7db2\u7d61\u722c\u87f2\u548c\u6578\u64da\u5206\u6790\u65b9\u9762\uff0cPython3\u64c1\u6709\u5982Beautiful Soup\u3001Requests\u548cScrapy\u7b49\u5f37\u5927\u7684\u5eab\uff0c\u9019\u4e9b\u5de5\u5177\u4f7f\u5f97\u6578\u64da\u7372\u53d6\u8b8a\u5f97\u66f4\u52a0\u9ad8\u6548\u548c\u4fbf\u6377\u3002<\/p>\n<h2 id=\"%e5%9f%ba%e6%9c%ac%e5%b7%a5%e5%85%b7%e4%bb%8b%e7%b4%b9-OQuMWbNFNb\">\u57fa\u672c\u5de5\u5177\u4ecb\u7d39<\/h2>\n<ul>\n<li><strong>Requests\uff1a<\/strong>\u9019\u662f\u4e00\u500b\u7c21\u55ae\u6613\u7528\u7684HTTP\u5eab\uff0c\u53ef\u4ee5\u7528\u4f86\u767c\u9001HTTP\u8acb\u6c42\uff0c\u7372\u53d6\u7db2\u9801\u5167\u5bb9\u3002<\/li>\n<li><strong>Beautiful Soup\uff1a<\/strong>\u9019\u662f\u4e00\u500b\u7528\u65bc\u89e3\u6790HTML\u548cXML\u6587\u6a94\u7684\u5eab\uff0c\u80fd\u5920\u8f15\u9b06\u5730\u63d0\u53d6\u6240\u9700\u7684\u6578\u64da\u3002<\/li>\n<li><strong>Scrapy\uff1a<\/strong>\u9019\u662f\u4e00\u500b\u529f\u80fd\u5f37\u5927\u7684\u7db2\u7d61\u722c\u87f2\u6846\u67b6\uff0c\u9069\u5408\u65bc\u5927\u898f\u6a21\u7684\u6578\u64da\u6293\u53d6\u4efb\u52d9\u3002<\/li>\n<\/ul>\n<h2 id=\"%e4%bd%bf%e7%94%a8requests%e7%8d%b2%e5%8f%96%e7%b6%b2%e9%a0%81%e6%95%b8%e6%93%9a-OQuMWbNFNb\">\u4f7f\u7528Requests\u7372\u53d6\u7db2\u9801\u6578\u64da<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5b89\u88ddRequests\u5eab\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u9032\u884c\u5b89\u88dd\uff1a<\/p>\n<pre><code>pip install requests<\/code><\/pre>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528Requests\u5eab\u4f86\u7372\u53d6\u7db2\u9801\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>import requests\n\nurl = 'https:\/\/example.com'\nresponse = requests.get(url)\n\nif response.status_code == 200:\n    print(response.text)\nelse:\n    print('\u7121\u6cd5\u7372\u53d6\u7db2\u9801\uff0c\u72c0\u614b\u78bc\uff1a', response.status_code)<\/code><\/pre>\n<h2 id=\"%e4%bd%bf%e7%94%a8beautiful-soup%e8%a7%a3%e6%9e%90%e6%95%b8%e6%93%9a-OQuMWbNFNb\">\u4f7f\u7528Beautiful Soup\u89e3\u6790\u6578\u64da<\/h2>\n<p>\u7372\u53d6\u5230\u7db2\u9801\u6578\u64da\u5f8c\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528Beautiful Soup\u4f86\u89e3\u6790HTML\u5167\u5bb9\u3002\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5b89\u88ddBeautiful Soup\u5eab\uff1a<\/p>\n<pre><code>pip install beautifulsoup4<\/code><\/pre>\n<p>\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528Beautiful Soup\u4f86\u63d0\u53d6\u7279\u5b9a\u6578\u64da\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>from bs4 import BeautifulSoup\n\nsoup = BeautifulSoup(response.text, 'html.parser')\ntitles = soup.find_all('h1')\n\nfor title in titles:\n    print(title.get_text())<\/code><\/pre>\n<h2 id=\"%e5%af%a6%e9%9a%9b%e6%87%89%e7%94%a8%e6%a1%88%e4%be%8b-OQuMWbNFNb\">\u5be6\u969b\u61c9\u7528\u6848\u4f8b<\/h2>\n<p>\u5047\u8a2d\u6211\u5011\u60f3\u8981\u5f9e\u4e00\u500b\u65b0\u805e\u7db2\u7ad9\u4e0a\u7372\u53d6\u6700\u65b0\u7684\u65b0\u805e\u6a19\u984c\u3002\u6211\u5011\u53ef\u4ee5\u7d50\u5408Requests\u548cBeautiful Soup\u4f86\u5be6\u73fe\uff1a<\/p>\n<pre><code>import requests\nfrom bs4 import BeautifulSoup\n\nurl = 'https:\/\/news.ycombinator.com\/'\nresponse = requests.get(url)\n\nif response.status_code == 200:\n    soup = BeautifulSoup(response.text, 'html.parser')\n    headlines = soup.find_all('a', class_='storylink')\n\n    for headline in headlines:\n        print(headline.get_text())\nelse:\n    print('\u7121\u6cd5\u7372\u53d6\u7db2\u9801\uff0c\u72c0\u614b\u78bc\uff1a', response.status_code)<\/code><\/pre>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-OQuMWbNFNb\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u5728\u9032\u884c\u7db2\u9801\u6578\u64da\u7372\u53d6\u6642\uff0c\u9700\u9075\u5faa\u7db2\u7ad9\u7684robots.txt\u898f\u7bc4\uff0c\u4e26\u5c0a\u91cd\u7db2\u7ad9\u7684\u4f7f\u7528\u689d\u6b3e\u3002\u6b64\u5916\uff0c\u904e\u65bc\u983b\u7e41\u7684\u8acb\u6c42\u53ef\u80fd\u6703\u5c0e\u81f4IP\u88ab\u5c01\u7981\uff0c\u56e0\u6b64\u5efa\u8b70\u5728\u722c\u53d6\u6578\u64da\u6642\u8a2d\u7f6e\u9069\u7576\u7684\u5ef6\u9072\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-OQuMWbNFNb\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528Python3\u7372\u53d6\u7db2\u9801\u6578\u64da\u5eab\u662f\u4e00\u500b\u7c21\u55ae\u800c\u6709\u6548\u7684\u904e\u7a0b\u3002\u901a\u904e\u7d50\u5408Requests\u548cBeautiful Soup\u7b49\u5eab\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u5f9e\u5404\u7a2e\u7db2\u7ad9\u4e2d\u63d0\u53d6\u6240\u9700\u7684\u6578\u64da\u3002\u7121\u8ad6\u662f\u9032\u884c\u6578\u64da\u5206\u6790\u9084\u662f\u69cb\u5efa\u6578\u64da\u9a45\u52d5\u7684\u61c9\u7528\uff0c\u638c\u63e1\u9019\u4e9b\u6280\u8853\u90fd\u5c07\u5927\u5927\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n<p>\u5982\u679c\u60a8\u9700\u8981\u7a69\u5b9a\u7684\u74b0\u5883\u4f86\u904b\u884c\u60a8\u7684Python\u61c9\u7528\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u670d\u52d9\uff0c\u9019\u5c07\u70ba\u60a8\u7684\u9805\u76ee\u63d0\u4f9b\u5f37\u5927\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528Python3\u7372\u53d6\u7db2\u9801\u6578\u64da\u5eab\uff0c\u638c\u63e1\u7db2\u7d61\u722c\u87f2\u6280\u8853\uff0c\u8f15\u9b06\u63d0\u53d6\u548c\u5206\u6790\u7db2\u9801\u6578\u64da\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-96982","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96982","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"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=96982"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96982\/revisions"}],"predecessor-version":[{"id":96983,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96982\/revisions\/96983"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=96982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=96982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=96982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}