{"id":192296,"date":"2024-11-12T18:34:19","date_gmt":"2024-11-12T10:34:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/192296\/"},"modified":"2024-11-12T18:34:21","modified_gmt":"2024-11-12T10:34:21","slug":"%e5%b0%8f%e8%b2%bc%e5%a3%ab%ef%bc%9a%e8%bc%95%e9%ac%86%e6%94%b6%e9%9b%86%e8%b6%85%e5%b8%82%e5%95%86%e5%93%81%e6%95%b8%e6%93%9a-%e5%a6%82%e4%bd%95%e5%bf%ab%e9%80%9f%e6%94%b6%e9%9b%86%e8%b6%85%e5%b8%82","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/192296\/","title":{"rendered":"\u5c0f\u8cbc\u58eb\uff1a\u8f15\u9b06\u6536\u96c6\u8d85\u5e02\u5546\u54c1\u6578\u64da (\u5982\u4f55\u5feb\u901f\u6536\u96c6\u8d85\u5e02\u5546\u54c1\u4fe1\u606f\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"%e5%b0%8f%e8%b2%bc%e5%a3%ab%ef%bc%9a%e8%bc%95%e9%ac%86%e6%94%b6%e9%9b%86%e8%b6%85%e5%b8%82%e5%95%86%e5%93%81%e6%95%b8%e6%93%9a-WTvLduVmsz\">\u5c0f\u8cbc\u58eb\uff1a\u8f15\u9b06\u6536\u96c6\u8d85\u5e02\u5546\u54c1\u6578\u64da<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u6642\u4ee3\uff0c\u6536\u96c6\u548c\u5206\u6790\u8d85\u5e02\u5546\u54c1\u6578\u64da\u5c0d\u65bc\u5546\u696d\u6c7a\u7b56\u81f3\u95dc\u91cd\u8981\u3002\u7121\u8ad6\u662f\u70ba\u4e86\u5e02\u5834\u7814\u7a76\u3001\u50f9\u683c\u6bd4\u8f03\u9084\u662f\u5eab\u5b58\u7ba1\u7406\uff0c\u64c1\u6709\u4e00\u500b\u5b8c\u6574\u7684\u5546\u54c1\u4fe1\u606f\u6578\u64da\u5eab\u90fd\u80fd\u5e6b\u52a9\u4f01\u696d\u66f4\u597d\u5730\u7406\u89e3\u5e02\u5834\u52d5\u614b\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u4e00\u4e9b\u6709\u6548\u7684\u65b9\u6cd5\uff0c\u5e6b\u52a9\u60a8\u5feb\u901f\u6536\u96c6\u8d85\u5e02\u5546\u54c1\u4fe1\u606f\u6578\u64da\u5eab\u3002<\/p>\n<h2 id=\"1-%e7%a2%ba%e5%ae%9a%e6%95%b8%e6%93%9a%e9%9c%80%e6%b1%82-WTvLduVmsz\">1. \u78ba\u5b9a\u6578\u64da\u9700\u6c42<\/h2>\n<p>\u5728\u958b\u59cb\u6536\u96c6\u6578\u64da\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u660e\u78ba\u60a8\u6240\u9700\u7684\u6578\u64da\u985e\u578b\u3002\u9019\u53ef\u80fd\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u5546\u54c1\u540d\u7a31<\/li>\n<li>\u50f9\u683c<\/li>\n<li>\u54c1\u724c<\/li>\n<li>\u5eab\u5b58\u72c0\u614b<\/li>\n<li>\u4fc3\u92b7\u4fe1\u606f<\/li>\n<\/ul>\n<p>\u78ba\u5b9a\u9019\u4e9b\u9700\u6c42\u5f8c\uff0c\u60a8\u53ef\u4ee5\u66f4\u6709\u91dd\u5c0d\u6027\u5730\u9032\u884c\u6578\u64da\u6536\u96c6\u3002<\/p>\n<h2 id=\"2-%e4%bd%bf%e7%94%a8%e7%b6%b2%e7%b5%a1%e7%88%ac%e8%9f%b2%e6%8a%80%e8%a1%93-WTvLduVmsz\">2. \u4f7f\u7528\u7db2\u7d61\u722c\u87f2\u6280\u8853<\/h2>\n<p>\u7db2\u7d61\u722c\u87f2\u662f\u4e00\u7a2e\u81ea\u52d5\u5316\u5de5\u5177\uff0c\u53ef\u4ee5\u5e6b\u52a9\u60a8\u5f9e\u7db2\u7ad9\u4e0a\u63d0\u53d6\u6578\u64da\u3002\u4f7f\u7528 Python \u7684 <code>BeautifulSoup<\/code> \u548c <code>requests<\/code> \u5eab\uff0c\u60a8\u53ef\u4ee5\u8f15\u9b06\u5730\u6293\u53d6\u8d85\u5e02\u7db2\u7ad9\u7684\u5546\u54c1\u4fe1\u606f\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>import requests\nfrom bs4 import BeautifulSoup\n\nurl = 'https:\/\/example-supermarket.com\/products'\nresponse = requests.get(url)\nsoup = BeautifulSoup(response.text, 'html.parser')\n\nfor product in soup.find_all('div', class_='product'):\n    name = product.find('h2').text\n    price = product.find('span', class_='price').text\n    print(f'\u5546\u54c1\u540d\u7a31: {name}, \u50f9\u683c: {price}')\n<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u5c07\u5f9e\u6307\u5b9a\u7684\u8d85\u5e02\u7db2\u7ad9\u6293\u53d6\u5546\u54c1\u540d\u7a31\u548c\u50f9\u683c\u3002\u6839\u64da\u7db2\u7ad9\u7684\u7d50\u69cb\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u8abf\u6574\u9078\u64c7\u5668\u3002<\/p>\n<h2 id=\"3-%e5%88%a9%e7%94%a8api%e6%8e%a5%e5%8f%a3-WTvLduVmsz\">3. \u5229\u7528API\u63a5\u53e3<\/h2>\n<p>\u8a31\u591a\u8d85\u5e02\u548c\u96fb\u5546\u5e73\u53f0\u63d0\u4f9bAPI\u63a5\u53e3\uff0c\u5141\u8a31\u958b\u767c\u8005\u7372\u53d6\u5546\u54c1\u6578\u64da\u3002\u4f7f\u7528API\u7684\u597d\u8655\u5728\u65bc\u6578\u64da\u7684\u6e96\u78ba\u6027\u548c\u5373\u6642\u6027\u3002\u60a8\u53ef\u4ee5\u901a\u904e\u8a3b\u518a\u7372\u53d6API\u5bc6\u9470\uff0c\u7136\u5f8c\u4f7f\u7528 <code>requests<\/code> \u5eab\u4f86\u8abf\u7528API\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>import requests\n\napi_url = 'https:\/\/api.example-supermarket.com\/products'\nheaders = {'Authorization': 'Bearer YOUR_API_KEY'}\nresponse = requests.get(api_url, headers=headers)\n\ndata = response.json()\nfor product in data['products']:\n    print(f'\u5546\u54c1\u540d\u7a31: {product[\"name\"]}, \u50f9\u683c: {product[\"price\"]}')\n<\/code><\/pre>\n<p>\u9019\u6a23\uff0c\u60a8\u5c31\u53ef\u4ee5\u8f15\u9b06\u7372\u53d6\u6700\u65b0\u7684\u5546\u54c1\u6578\u64da\u3002<\/p>\n<h2 id=\"4-%e6%95%b8%e6%93%9a%e6%b8%85%e7%90%86%e8%88%87%e6%95%b4%e7%90%86-WTvLduVmsz\">4. \u6578\u64da\u6e05\u7406\u8207\u6574\u7406<\/h2>\n<p>\u6536\u96c6\u5230\u7684\u6578\u64da\u5f80\u5f80\u9700\u8981\u9032\u884c\u6e05\u7406\u548c\u6574\u7406\uff0c\u4ee5\u4fbf\u65bc\u5f8c\u7e8c\u5206\u6790\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>Pandas<\/code> \u5eab\u4f86\u8655\u7406\u6578\u64da\uff0c\u4f8b\u5982\u53bb\u9664\u91cd\u8907\u9805\u3001\u586b\u88dc\u7f3a\u5931\u503c\u7b49\uff1a<\/p>\n<pre><code>import pandas as pd\n\n# \u5047\u8a2d\u60a8\u5df2\u7d93\u6709\u4e00\u500b\u6578\u64da\u6846 df\ndf = pd.DataFrame(data)\n\n# \u53bb\u9664\u91cd\u8907\u9805\ndf.drop_duplicates(inplace=True)\n\n# \u586b\u88dc\u7f3a\u5931\u503c\ndf.fillna('\u672a\u77e5', inplace=True)\n\nprint(df.head())\n<\/code><\/pre>\n<h2 id=\"5-%e6%95%b8%e6%93%9a%e5%ad%98%e5%84%b2%e8%88%87%e7%ae%a1%e7%90%86-WTvLduVmsz\">5. \u6578\u64da\u5b58\u5132\u8207\u7ba1\u7406<\/h2>\n<p>\u6700\u5f8c\uff0c\u5c07\u6e05\u7406\u5f8c\u7684\u6578\u64da\u5b58\u5132\u5728\u5408\u9069\u7684\u6578\u64da\u5eab\u4e2d\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u60a8\u53ef\u4ee5\u9078\u64c7\u4f7f\u7528\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff08\u5982 MySQL\uff09\u6216 NoSQL \u6578\u64da\u5eab\uff08\u5982 MongoDB\uff09\uff0c\u6839\u64da\u60a8\u7684\u9700\u6c42\u9032\u884c\u9078\u64c7\u3002\u78ba\u4fdd\u6578\u64da\u5eab\u7684\u7d50\u69cb\u80fd\u5920\u652f\u6301\u672a\u4f86\u7684\u64f4\u5c55\u548c\u67e5\u8a62\u9700\u6c42\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-WTvLduVmsz\">\u7e3d\u7d50<\/h2>\n<p>\u6536\u96c6\u8d85\u5e02\u5546\u54c1\u6578\u64da\u7684\u904e\u7a0b\u96d6\u7136\u8907\u96dc\uff0c\u4f46\u901a\u904e\u660e\u78ba\u9700\u6c42\u3001\u4f7f\u7528\u7db2\u7d61\u722c\u87f2\u548cAPI\u3001\u9032\u884c\u6578\u64da\u6e05\u7406\u8207\u6574\u7406\uff0c\u60a8\u53ef\u4ee5\u6709\u6548\u5730\u5efa\u7acb\u4e00\u500b\u5546\u54c1\u4fe1\u606f\u6578\u64da\u5eab\u3002\u9019\u4e0d\u50c5\u80fd\u5e6b\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u5e02\u5834\uff0c\u9084\u80fd\u63d0\u5347\u696d\u52d9\u6c7a\u7b56\u7684\u6e96\u78ba\u6027\u3002\u5982\u679c\u60a8\u9700\u8981\u7a69\u5b9a\u7684\u6578\u64da\u5b58\u5132\u548c\u7ba1\u7406\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u6216<a href=\"https:\/\/server.hk\">\u4e91\u670d\u52a1\u5668<\/a>\u4f86\u652f\u6301\u60a8\u7684\u6578\u64da\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f15\u9b06\u6536\u96c6\u8d85\u5e02\u5546\u54c1\u6578\u64da\u7684\u5be6\u7528\u5c0f\u8cbc\u58eb\uff0c\u5feb\u901f\u5efa\u7acb\u5546\u54c1\u4fe1\u606f\u6578\u64da\u5eab\uff0c\u63d0\u5347\u60a8\u7684\u5e02\u5834\u5206\u6790\u6548\u7387\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-192296","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/192296","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=192296"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/192296\/revisions"}],"predecessor-version":[{"id":192297,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/192296\/revisions\/192297"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=192296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=192296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=192296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}