{"id":3251,"date":"2024-11-10T09:00:38","date_gmt":"2024-11-10T01:00:38","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3251\/"},"modified":"2024-11-10T09:00:38","modified_gmt":"2024-11-10T01:00:38","slug":"%e5%a6%82%e4%bd%95%e7%94%a8js%e5%86%99%e7%88%ac%e8%99%ab","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3251\/","title":{"rendered":"\u5982\u4f55\u7528js\u5199\u722c\u866b"},"content":{"rendered":"<blockquote><p>\n  javascript \u53ef\u7528\u4e8e\u7f16\u5199\u722c\u866b\uff0c\u6b65\u9aa4\u5305\u62ec\uff1a\u53d1\u51fa http \u8bf7\u6c42\uff1b\u89e3\u6790 html \u54cd\u5e94\uff1b\u63d0\u53d6\u6240\u9700\u6570\u636e\uff1b\u4fdd\u5b58\u6570\u636e\u3002javascript \u722c\u866b\u5177\u6709\u4e30\u5bcc\u7684\u751f\u6001\u7cfb\u7edf\uff0c\u4fbf\u4e8e\u96c6\u6210\uff0c\u4f46\u53ef\u80fd\u88ab\u68c0\u6d4b\u6216\u5904\u7406 javascript \u6e32\u67d3\u7f51\u7ad9\u65f6\u4f1a\u66f4\u56f0\u96be\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110808213395017.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u7528js\u5199\u722c\u866b\u63d2\u56fe\" alt=\"\u5982\u4f55\u7528js\u5199\u722c\u866b\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u7528 JavaScript \u7f16\u5199\u722c\u866b<\/strong><\/p>\n<p>JavaScript \u53ef\u4ee5\u7528\u6765\u7f16\u5199\u722c\u866b\uff0c\u8fd9\u662f\u4e00\u79cd\u5728\u7f51\u7edc\u4e0a\u81ea\u52a8\u91c7\u96c6\u548c\u63d0\u53d6\u6570\u636e\u7684\u7a0b\u5e8f\u3002<\/p>\n<p><strong>\u4f7f\u7528 JavaScript \u7f16\u5199\u722c\u866b\u7684\u6b65\u9aa4\uff1a<\/strong><\/p>\n<p><strong>1. \u8bf7\u6c42\u6570\u636e<\/strong><\/p>\n<p>\u4f7f\u7528 fetch API \u53d1\u51fa HTTP \u8bf7\u6c42\u4ee5\u83b7\u53d6\u6240\u9700\u9875\u9762\u3002<\/p>\n<pre>const response = await fetch('https:\/\/example.com');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>2. \u89e3\u6790\u54cd\u5e94<\/strong><\/p>\n<p>\u4f7f\u7528 DOMParser API \u89e3\u6790 HTML \u54cd\u5e94\u3002<\/p>\n<pre>const document = new DOMParser().parseFromString(response.text(), 'text\/html');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>3. \u63d0\u53d6\u6570\u636e<\/strong><\/p>\n<p>\u4f7f\u7528 JavaScript \u9009\u62e9\u5668\u63d0\u53d6\u6240\u9700\u6570\u636e\u3002<\/p>\n<pre>const title = document.querySelector('title').textContent;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>4. \u4fdd\u5b58\u6570\u636e<\/strong><\/p>\n<p>\u5c06\u6536\u96c6\u7684\u6570\u636e\u5b58\u50a8\u5728\u672c\u5730\u6587\u4ef6\u6216\u6570\u636e\u5e93\u4e2d\u3002<\/p>\n<pre>const fs = require('fs');\nfs.writeFileSync('data.txt', title);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684 JavaScript \u722c\u866b\u793a\u4f8b\uff0c\u7528\u4e8e\u4ece\u7ed9\u5b9a URL \u4e2d\u63d0\u53d6\u6807\u9898\uff1a<\/p>\n<pre>const fetch = require('node-fetch');\n\nconst url = 'https:\/\/example.com';\n\nasync function crawl() {\n  const response = await fetch(url);\n  const document = new DOMParser().parseFromString(response.text(), 'text\/html');\n  const title = document.querySelector('title').textContent;\n  console.log(title);\n}\n\ncrawl();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<ul>\n<li>JavaScript \u662f\u4e00\u79cd\u5e7f\u6cdb\u4f7f\u7528\u7684\u8bed\u8a00\uff0c\u5177\u6709\u4e30\u5bcc\u7684\u751f\u6001\u7cfb\u7edf\u3002<\/li>\n<li>\u53ef\u4ee5\u4f7f\u7528 JavaScript \u6846\u67b6\uff08\u5982 Puppeteer\uff09\u7b80\u5316\u722c\u866b\u7684\u7f16\u5199\u8fc7\u7a0b\u3002<\/li>\n<li>JavaScript \u722c\u866b\u53ef\u4ee5\u8f7b\u677e\u5730\u4e0e\u5176\u4ed6 JavaScript \u5e94\u7528\u7a0b\u5e8f\u96c6\u6210\u3002<\/li>\n<\/ul>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<ul>\n<li>JavaScript \u722c\u866b\u53ef\u4ee5\u88ab JavaScript \u7981\u7528\u6216\u68c0\u6d4b\u5230\u3002<\/li>\n<li>JavaScript \u722c\u866b\u5bf9 JavaScript \u6e32\u67d3\u7684\u7f51\u7ad9\u5904\u7406\u8d77\u6765\u53ef\u80fd\u4f1a\u66f4\u52a0\u56f0\u96be\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u7528js\u5199\u722c\u866b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>javascript \u53ef\u7528\u4e8e\u7f16\u5199\u722c\u866b\uff0c\u6b65\u9aa4\u5305\u62ec\uff1a\u53d1\u51fa http \u8bf7\u6c42\uff1b\u89e3\u6790 html \u54cd\u5e94\uff1b\u63d0\u53d6\u6240\u9700\u6570\u636e\uff1b\u4fdd\u5b58\u6570\u636e\u3002javascript \u722c\u866b\u5177\u6709\u4e30\u5bcc\u7684\u751f\u6001\u7cfb\u7edf\uff0c\u4fbf\u4e8e\u96c6\u6210\uff0c\u4f46\u53ef\u80fd\u88ab\u68c0\u6d4b\u6216\u5904\u7406 javascript \u6e32\u67d3\u7f51\u7ad9\u65f6\u4f1a\u66f4\u56f0\u96be\u3002 \u5982\u4f55\u7528 JavaScript \u7f16\u5199\u722c\u866b JavaScript \u53ef\u4ee5\u7528\u6765\u7f16\u5199\u722c\u866b\uff0c\u8fd9\u662f\u4e00\u79cd\u5728\u7f51\u7edc\u4e0a\u81ea\u52a8\u91c7\u96c6\u548c\u63d0\u53d6\u6570\u636e\u7684\u7a0b\u5e8f\u3002 \u4f7f\u7528 JavaScript \u7f16\u5199\u722c\u866b\u7684\u6b65\u9aa4\uff1a 1. \u8bf7\u6c42\u6570\u636e \u4f7f\u7528 fetch API \u53d1\u51fa HTTP \u8bf7\u6c42\u4ee5\u83b7\u53d6\u6240\u9700\u9875\u9762\u3002 const response = await fetch(&#8216;https:\/\/example.com&#8217;); \u767b\u5f55\u540e\u590d\u5236 2. \u89e3\u6790\u54cd\u5e94 \u4f7f\u7528 DOMParser API \u89e3\u6790 HTML \u54cd\u5e94\u3002 const document = new DOMParser().parseFromString(response.text(), &#8216;text\/html&#8217;); \u767b\u5f55\u540e\u590d\u5236 3. \u63d0\u53d6\u6570\u636e \u4f7f\u7528 JavaScript \u9009\u62e9\u5668\u63d0\u53d6\u6240\u9700\u6570\u636e\u3002 const title = document.querySelector(&#8216;title&#8217;).textContent; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3251","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3251","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=3251"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3251\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}