用基用js在VS code上面实现获取百度搜索页面源代码的html部分
-
创建一个新的文件夹,并在该文件夹中创建一个名为
main.js
的文件。 -
在
main.js
中输入以下代码:
const https = require('https');const options = {hostname: 'www.baidu.com',path: '/s?wd=Hello%20World',method: 'GET',
};const req = https.request(options, (res) => {let data = '';res.on('data', (chunk) => {data += chunk;});res.on('end', () => {const html = data.match(/<html\b[^>]*>([\s\S]*?)<\/html>/)[0];console.log(html);});
});req.end();
- 在VS Code中打开终端(Terminal),然后通过运行以下命令来执行脚本:
node main.js
- 如果一切顺利,你将会在终端看到从百度搜索页面获取的HTML部分的输出。