{"id":81318,"date":"2024-10-16T15:19:02","date_gmt":"2024-10-16T07:19:02","guid":{"rendered":"https:\/\/server.hk\/cnblog\/81318\/"},"modified":"2024-10-16T15:19:03","modified_gmt":"2024-10-16T07:19:03","slug":"node%e6%8a%80%e8%a1%93%e5%af%a6%e7%8f%be%e7%be%8e%e5%9c%98%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e6%95%b8%e6%93%9a%e7%88%ac%e5%8f%96-node%e6%80%8e%e9%ba%bc%e7%88%ac%e5%8f%96%e7%be%8e%e5%9c%98%e6%95%b8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/81318\/","title":{"rendered":"Node\u6280\u8853\u5be6\u73fe\u7f8e\u5718\u6578\u64da\u5eab\u7684\u6578\u64da\u722c\u53d6 (Node\u600e\u9ebc\u722c\u53d6\u7f8e\u5718\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"node%e6%8a%80%e8%a1%93%e5%af%a6%e7%8f%be%e7%be%8e%e5%9c%98%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e6%95%b8%e6%93%9a%e7%88%ac%e5%8f%96-ftilAaKlXP\">Node\u6280\u8853\u5be6\u73fe\u7f8e\u5718\u6578\u64da\u5eab\u7684\u6578\u64da\u722c\u53d6<\/h1>\n<p>\u96a8\u8457\u4e92\u806f\u7db2\u7684\u8fc5\u901f\u767c\u5c55\uff0c\u6578\u64da\u722c\u53d6\u6280\u8853\u5728\u5404\u884c\u5404\u696d\u4e2d\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u91cd\u8981\u3002\u7279\u5225\u662f\u5728\u9910\u98f2\u548c\u5916\u8ce3\u884c\u696d\uff0c\u7f8e\u5718\u4f5c\u70ba\u4e2d\u570b\u6700\u5927\u7684\u5916\u8ce3\u5e73\u53f0\u4e4b\u4e00\uff0c\u64c1\u6709\u6d77\u91cf\u7684\u6578\u64da\u8cc7\u6e90\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528Node.js\u6280\u8853\u4f86\u5be6\u73fe\u5c0d\u7f8e\u5718\u6578\u64da\u5eab\u7684\u6578\u64da\u722c\u53d6\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u4ee3\u78bc\u793a\u4f8b\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e6%95%b8%e6%93%9a%e7%88%ac%e5%8f%96%ef%bc%9f-ftilAaKlXP\">\u4ec0\u9ebc\u662f\u6578\u64da\u722c\u53d6\uff1f<\/h2>\n<p>\u6578\u64da\u722c\u53d6\u662f\u6307\u81ea\u52d5\u5316\u5730\u5f9e\u7db2\u7ad9\u4e0a\u63d0\u53d6\u6578\u64da\u7684\u904e\u7a0b\u3002\u9019\u4e00\u904e\u7a0b\u901a\u5e38\u6d89\u53ca\u5230HTTP\u8acb\u6c42\u3001HTML\u89e3\u6790\u548c\u6578\u64da\u5b58\u5132\u7b49\u6b65\u9a5f\u3002\u5c0d\u65bc\u958b\u767c\u8005\u4f86\u8aaa\uff0c\u6578\u64da\u722c\u53d6\u53ef\u4ee5\u5e6b\u52a9\u4ed6\u5011\u7372\u53d6\u7af6\u722d\u5c0d\u624b\u7684\u4fe1\u606f\u3001\u5206\u6790\u5e02\u5834\u8da8\u52e2\u6216\u9032\u884c\u6578\u64da\u6316\u6398\u3002<\/p>\n<h2 id=\"node-js%e7%9a%84%e5%84%aa%e5%8b%a2-ftilAaKlXP\">Node.js\u7684\u512a\u52e2<\/h2>\n<p>Node.js\u662f\u4e00\u500b\u57fa\u65bcChrome V8\u5f15\u64ce\u7684JavaScript\u904b\u884c\u74b0\u5883\uff0c\u5177\u6709\u975e\u963b\u585eI\/O\u548c\u4e8b\u4ef6\u9a45\u52d5\u7684\u7279\u6027\uff0c\u9019\u4f7f\u5f97\u5b83\u975e\u5e38\u9069\u5408\u7528\u65bc\u6578\u64da\u722c\u53d6\u3002\u4ee5\u4e0b\u662fNode.js\u7684\u4e00\u4e9b\u512a\u52e2\uff1a<\/p>\n<ul>\n<li><strong>\u9ad8\u6548\u80fd\uff1a<\/strong>Node.js\u80fd\u5920\u8655\u7406\u5927\u91cf\u7684\u4e26\u767c\u8acb\u6c42\uff0c\u9019\u5c0d\u65bc\u722c\u53d6\u5927\u91cf\u6578\u64da\u975e\u5e38\u91cd\u8981\u3002<\/li>\n<li><strong>\u8c50\u5bcc\u7684\u751f\u614b\u7cfb\u7d71\uff1a<\/strong>Node.js\u64c1\u6709\u5927\u91cf\u7684\u7b2c\u4e09\u65b9\u5eab\u548c\u5de5\u5177\uff0c\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u5feb\u901f\u5be6\u73fe\u722c\u53d6\u529f\u80fd\u3002<\/li>\n<li><strong>\u6613\u65bc\u5b78\u7fd2\uff1a<\/strong>\u5c0d\u65bc\u719f\u6089JavaScript\u7684\u958b\u767c\u8005\u4f86\u8aaa\uff0cNode.js\u7684\u5b78\u7fd2\u66f2\u7dda\u76f8\u5c0d\u8f03\u5e73\u7de9\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8node-js%e7%88%ac%e5%8f%96%e7%be%8e%e5%9c%98%e6%95%b8%e6%93%9a-ftilAaKlXP\">\u5982\u4f55\u4f7f\u7528Node.js\u722c\u53d6\u7f8e\u5718\u6578\u64da<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528Node.js\u722c\u53d6\u7f8e\u5718\u6578\u64da\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e5%ae%89%e8%a3%9d%e5%bf%85%e8%a6%81%e7%9a%84%e5%ba%ab-ftilAaKlXP\">1. \u5b89\u88dd\u5fc5\u8981\u7684\u5eab<\/h3>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5b89\u88dd\u4e00\u4e9b\u5fc5\u8981\u7684\u5eab\uff0c\u4f8b\u5982axios\u548ccheerio\u3002axios\u7528\u65bc\u767c\u9001HTTP\u8acb\u6c42\uff0c\u800ccheerio\u5247\u7528\u65bc\u89e3\u6790HTML\u3002<\/p>\n<pre><code>npm install axios cheerio<\/code><\/pre>\n<h3 id=\"2-%e7%99%bc%e9%80%81http%e8%ab%8b%e6%b1%82-ftilAaKlXP\">2. \u767c\u9001HTTP\u8acb\u6c42<\/h3>\n<p>\u4f7f\u7528axios\u767c\u9001\u8acb\u6c42\u4ee5\u7372\u53d6\u7f8e\u5718\u7684\u7db2\u9801\u6578\u64da\uff1a<\/p>\n<pre><code>const axios = require('axios');\n\nasync function fetchData(url) {\n    try {\n        const response = await axios.get(url);\n        return response.data;\n    } catch (error) {\n        console.error(error);\n    }\n}\n\nconst url = 'https:\/\/www.meituan.com\/';\nfetchData(url).then(data =&gt; {\n    console.log(data);\n});<\/code><\/pre>\n<h3 id=\"3-%e8%a7%a3%e6%9e%90html%e6%95%b8%e6%93%9a-ftilAaKlXP\">3. \u89e3\u6790HTML\u6578\u64da<\/h3>\n<p>\u4f7f\u7528cheerio\u4f86\u89e3\u6790\u7372\u53d6\u7684HTML\u6578\u64da\uff0c\u4e26\u63d0\u53d6\u6240\u9700\u7684\u4fe1\u606f\uff1a<\/p>\n<pre><code>const cheerio = require('cheerio');\n\nfunction parseData(html) {\n    const $ = cheerio.load(html);\n    const items = [];\n    \n    $('.item-selector').each((index, element) =&gt; {\n        const title = $(element).find('.title-selector').text();\n        const price = $(element).find('.price-selector').text();\n        items.push({ title, price });\n    });\n    \n    return items;\n}\n\nfetchData(url).then(data =&gt; {\n    const parsedData = parseData(data);\n    console.log(parsedData);\n});<\/code><\/pre>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-ftilAaKlXP\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u5728\u9032\u884c\u6578\u64da\u722c\u53d6\u6642\uff0c\u958b\u767c\u8005\u9700\u8981\u9075\u5faa\u7db2\u7ad9\u7684robots.txt\u898f\u7bc4\uff0c\u4e26\u9075\u5b88\u76f8\u95dc\u6cd5\u5f8b\u6cd5\u898f\u3002\u6b64\u5916\uff0c\u904e\u65bc\u983b\u7e41\u7684\u8acb\u6c42\u53ef\u80fd\u6703\u5c0e\u81f4IP\u88ab\u5c01\u7981\uff0c\u56e0\u6b64\u5efa\u8b70\u4f7f\u7528\u5ef6\u9072\u8acb\u6c42\u6216\u4ee3\u7406\u670d\u52d9\u4f86\u6e1b\u5c11\u98a8\u96aa\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-ftilAaKlXP\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528Node.js\u9032\u884c\u7f8e\u5718\u6578\u64da\u5eab\u7684\u6578\u64da\u722c\u53d6\u662f\u4e00\u500b\u9ad8\u6548\u4e14\u9748\u6d3b\u7684\u89e3\u6c7a\u65b9\u6848\u3002\u901a\u904e\u7c21\u55ae\u7684\u4ee3\u78bc\u793a\u4f8b\uff0c\u6211\u5011\u53ef\u4ee5\u770b\u5230\u5982\u4f55\u767c\u9001\u8acb\u6c42\u3001\u89e3\u6790\u6578\u64da\u4e26\u63d0\u53d6\u6240\u9700\u7684\u4fe1\u606f\u3002\u96a8\u8457\u6578\u64da\u9700\u6c42\u7684\u589e\u52a0\uff0c\u638c\u63e1\u9019\u9805\u6280\u8853\u5c07\u5c0d\u958b\u767c\u8005\u548c\u4f01\u696d\u5e36\u4f86\u5de8\u5927\u7684\u50f9\u503c\u3002\u5982\u679c\u60a8\u9700\u8981\u7a69\u5b9a\u7684\u74b0\u5883\u4f86\u904b\u884c\u60a8\u7684\u722c\u87f2\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u670d\u52d9\uff0c\u4ee5\u78ba\u4fdd\u60a8\u7684\u6578\u64da\u722c\u53d6\u4efb\u52d9\u80fd\u5920\u9806\u5229\u9032\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u4f7f\u7528Node\u6280\u8853\u9ad8\u6548\u722c\u53d6\u7f8e\u5718\u6578\u64da\u5eab\uff0c\u5be6\u73fe\u6578\u64da\u63d0\u53d6\u8207\u5206\u6790\uff0c\u63d0\u5347\u696d\u52d9\u6c7a\u7b56\u80fd\u529b\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-81318","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/81318","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=81318"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/81318\/revisions"}],"predecessor-version":[{"id":81319,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/81318\/revisions\/81319"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=81318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=81318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=81318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}