{"id":190156,"date":"2024-11-12T10:14:19","date_gmt":"2024-11-12T02:14:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/190156\/"},"modified":"2024-11-12T10:14:20","modified_gmt":"2024-11-12T02:14:20","slug":"%e7%b6%b2%e9%a0%81%e6%8a%93%e5%8f%96%e6%8a%80%e5%b7%a7%ef%bc%9a%e6%95%b8%e6%93%9a%e5%bf%ab%e9%80%9f%e5%85%a5%e5%ba%ab-%e7%b6%b2%e9%a0%81%e6%8a%93%e5%8f%96%e4%b8%a6%e5%af%ab%e5%85%a5%e6%95%b8%e6%93%9a","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/190156\/","title":{"rendered":"\u7db2\u9801\u6293\u53d6\u6280\u5de7\uff1a\u6578\u64da\u5feb\u901f\u5165\u5eab (\u7db2\u9801\u6293\u53d6\u4e26\u5beb\u5165\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"%e7%b6%b2%e9%a0%81%e6%8a%93%e5%8f%96%e6%8a%80%e5%b7%a7%ef%bc%9a%e6%95%b8%e6%93%9a%e5%bf%ab%e9%80%9f%e5%85%a5%e5%ba%ab-%e7%b6%b2%e9%a0%81%e6%8a%93%e5%8f%96%e4%b8%a6%e5%af%ab%e5%85%a5%e6%95%b8%e6%93%9a-wSvslcxyyf\">\u7db2\u9801\u6293\u53d6\u6280\u5de7\uff1a\u6578\u64da\u5feb\u901f\u5165\u5eab (\u7db2\u9801\u6293\u53d6\u4e26\u5beb\u5165\u6578\u64da\u5eab)<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u6642\u4ee3\uff0c\u7db2\u9801\u6293\u53d6\u6280\u8853\u6210\u70ba\u4e86\u8a31\u591a\u4f01\u696d\u548c\u958b\u767c\u8005\u7684\u91cd\u8981\u5de5\u5177\u3002\u900f\u904e\u7db2\u9801\u6293\u53d6\uff0c\u6211\u5011\u53ef\u4ee5\u81ea\u52d5\u5316\u5730\u5f9e\u7db2\u7ad9\u4e0a\u63d0\u53d6\u6578\u64da\uff0c\u4e26\u5c07\u5176\u5b58\u5132\u5230\u6578\u64da\u5eab\u4e2d\uff0c\u4ee5\u4fbf\u9032\u4e00\u6b65\u5206\u6790\u548c\u4f7f\u7528\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u7db2\u9801\u6293\u53d6\u7684\u57fa\u672c\u6982\u5ff5\u3001\u6280\u8853\u5be6\u73fe\u4ee5\u53ca\u5982\u4f55\u5c07\u6293\u53d6\u5230\u7684\u6578\u64da\u5feb\u901f\u5165\u5eab\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e7%b6%b2%e9%a0%81%e6%8a%93%e5%8f%96%ef%bc%9f-wSvslcxyyf\">\u4ec0\u9ebc\u662f\u7db2\u9801\u6293\u53d6\uff1f<\/h2>\n<p>\u7db2\u9801\u6293\u53d6\uff08Web Scraping\uff09\u662f\u6307\u81ea\u52d5\u63d0\u53d6\u7db2\u7ad9\u4e0a\u4fe1\u606f\u7684\u904e\u7a0b\u3002\u9019\u4e00\u904e\u7a0b\u901a\u5e38\u6d89\u53ca\u5230\u4f7f\u7528\u7a0b\u5f0f\u78bc\u4f86\u6a21\u64ec\u4eba\u985e\u7528\u6236\u7684\u884c\u70ba\uff0c\u8a2a\u554f\u7db2\u9801\u4e26\u63d0\u53d6\u6240\u9700\u7684\u6578\u64da\u3002\u7db2\u9801\u6293\u53d6\u53ef\u4ee5\u7528\u65bc\u5404\u7a2e\u7528\u9014\uff0c\u4f8b\u5982\u5e02\u5834\u8abf\u67e5\u3001\u7af6\u722d\u5206\u6790\u3001\u6578\u64da\u805a\u5408\u7b49\u3002<\/p>\n<h2 id=\"%e7%b6%b2%e9%a0%81%e6%8a%93%e5%8f%96%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%b5%81%e7%a8%8b-wSvslcxyyf\">\u7db2\u9801\u6293\u53d6\u7684\u57fa\u672c\u6d41\u7a0b<\/h2>\n<ul>\n<li><strong>\u767c\u9001\u8acb\u6c42\uff1a<\/strong>\u4f7f\u7528HTTP\u8acb\u6c42\u8a2a\u554f\u76ee\u6a19\u7db2\u7ad9\u3002<\/li>\n<li><strong>\u7372\u53d6\u97ff\u61c9\uff1a<\/strong>\u63a5\u6536\u7db2\u7ad9\u8fd4\u56de\u7684HTML\u5167\u5bb9\u3002<\/li>\n<li><strong>\u89e3\u6790\u6578\u64da\uff1a<\/strong>\u4f7f\u7528\u89e3\u6790\u5eab\u63d0\u53d6\u6240\u9700\u7684\u6578\u64da\u3002<\/li>\n<li><strong>\u6578\u64da\u5b58\u5132\uff1a<\/strong>\u5c07\u63d0\u53d6\u7684\u6578\u64da\u5beb\u5165\u6578\u64da\u5eab\u3002<\/li>\n<\/ul>\n<h2 id=\"%e6%8a%80%e8%a1%93%e5%af%a6%e7%8f%be-wSvslcxyyf\">\u6280\u8853\u5be6\u73fe<\/h2>\n<p>\u5728\u5be6\u73fe\u7db2\u9801\u6293\u53d6\u6642\uff0c\u5e38\u7528\u7684\u7de8\u7a0b\u8a9e\u8a00\u5305\u62ecPython\u3001JavaScript\u548cRuby\u7b49\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Python\u9032\u884c\u7db2\u9801\u6293\u53d6\u7684\u57fa\u672c\u793a\u4f8b\uff1a<\/p>\n<pre><code>import requests\nfrom bs4 import BeautifulSoup\n\n# \u767c\u9001\u8acb\u6c42\nurl = 'https:\/\/example.com'\nresponse = requests.get(url)\n\n# \u78ba\u8a8d\u8acb\u6c42\u6210\u529f\nif response.status_code == 200:\n    # \u89e3\u6790HTML\n    soup = BeautifulSoup(response.text, 'html.parser')\n    \n    # \u63d0\u53d6\u6578\u64da\n    titles = soup.find_all('h2')\n    for title in titles:\n        print(title.text)\n<\/code><\/pre>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%bf%ab%e9%80%9f%e5%85%a5%e5%ba%ab-wSvslcxyyf\">\u6578\u64da\u5feb\u901f\u5165\u5eab<\/h2>\n<p>\u5728\u6293\u53d6\u5230\u6240\u9700\u6578\u64da\u5f8c\uff0c\u4e0b\u4e00\u6b65\u662f\u5c07\u9019\u4e9b\u6578\u64da\u5b58\u5132\u5230\u6578\u64da\u5eab\u4e2d\u3002\u5e38\u898b\u7684\u6578\u64da\u5eab\u6709MySQL\u3001PostgreSQL\u548cMongoDB\u7b49\u3002\u4ee5\u4e0b\u662f\u5c07\u6293\u53d6\u5230\u7684\u6578\u64da\u5beb\u5165MySQL\u6578\u64da\u5eab\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>import mysql.connector\n\n# \u9023\u63a5\u5230\u6578\u64da\u5eab\ndb = mysql.connector.connect(\n    host='localhost',\n    user='yourusername',\n    password='yourpassword',\n    database='yourdatabase'\n)\n\ncursor = db.cursor()\n\n# \u63d2\u5165\u6578\u64da\nfor title in titles:\n    sql = \"INSERT INTO articles (title) VALUES (%s)\"\n    val = (title.text,)\n    cursor.execute(sql, val)\n\n# \u63d0\u4ea4\u8b8a\u66f4\ndb.commit()\nprint(cursor.rowcount, \"record inserted.\")\n\n# \u95dc\u9589\u9023\u63a5\ncursor.close()\ndb.close()\n<\/code><\/pre>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-wSvslcxyyf\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u5728\u9032\u884c\u7db2\u9801\u6293\u53d6\u6642\uff0c\u9700\u6ce8\u610f\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li><strong>\u9075\u5b88\u7db2\u7ad9\u7684robots.txt\u898f\u7bc4\uff1a<\/strong>\u8a31\u591a\u7db2\u7ad9\u6703\u5728\u5176\u6839\u76ee\u9304\u4e0b\u63d0\u4f9brobots.txt\u6587\u4ef6\uff0c\u6307\u660e\u54ea\u4e9b\u90e8\u5206\u53ef\u4ee5\u88ab\u6293\u53d6\uff0c\u54ea\u4e9b\u90e8\u5206\u4e0d\u53ef\u4ee5\u3002<\/li>\n<li><strong>\u907f\u514d\u904e\u65bc\u983b\u7e41\u7684\u8acb\u6c42\uff1a<\/strong>\u904e\u65bc\u983b\u7e41\u7684\u8acb\u6c42\u53ef\u80fd\u6703\u5c0e\u81f4IP\u88ab\u5c01\u7981\uff0c\u5efa\u8b70\u4f7f\u7528\u5ef6\u9072\u8acb\u6c42\u7684\u65b9\u5f0f\u3002<\/li>\n<li><strong>\u6578\u64da\u7684\u5408\u6cd5\u6027\uff1a<\/strong>\u78ba\u4fdd\u6293\u53d6\u7684\u6578\u64da\u4e0d\u4fb5\u72af\u7248\u6b0a\u6216\u5176\u4ed6\u6cd5\u5f8b\u554f\u984c\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-wSvslcxyyf\">\u7e3d\u7d50<\/h2>\n<p>\u7db2\u9801\u6293\u53d6\u662f\u4e00\u9805\u5f37\u5927\u7684\u6280\u8853\uff0c\u80fd\u5920\u5e6b\u52a9\u4f01\u696d\u548c\u958b\u767c\u8005\u81ea\u52d5\u5316\u6578\u64da\u6536\u96c6\u7684\u904e\u7a0b\u3002\u900f\u904e\u9069\u7576\u7684\u5de5\u5177\u548c\u6280\u8853\uff0c\u5c07\u6293\u53d6\u5230\u7684\u6578\u64da\u5feb\u901f\u5165\u5eab\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002\u82e5\u60a8\u9700\u8981\u7a69\u5b9a\u7684\u74b0\u5883\u4f86\u9032\u884c\u7db2\u9801\u6293\u53d6\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u6216<a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a>\uff0c\u4ee5\u78ba\u4fdd\u60a8\u7684\u6293\u53d6\u4efb\u52d9\u9806\u5229\u9032\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u638c\u63e1\u7db2\u9801\u6293\u53d6\u6280\u5de7\uff0c\u5feb\u901f\u5c07\u6578\u64da\u63d0\u53d6\u4e26\u5beb\u5165\u6578\u64da\u5eab\uff0c\u63d0\u5347\u6578\u64da\u8655\u7406\u6548\u7387\uff0c\u52a9\u529b\u696d\u52d9\u6c7a\u7b56\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-190156","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/190156","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=190156"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/190156\/revisions"}],"predecessor-version":[{"id":190157,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/190156\/revisions\/190157"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=190156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=190156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=190156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}