很高兴为您提供一个基于 Node.js 的爬虫脚本示例:

const request = require('request');
const cheerio = require('cheerio');

request('https://www.example.com', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    const $ = cheerio.load(body);
    $('a').each(function(i, element){
      const link = $(element).attr('href');
      console.log(link);
    });
  }
});

这个简单的脚本使用了 Request 和 Cheerio 模块。它请求了一个网站的主页,然后使用 Cheerio 解析 HTML,并找到所有链接并输出到控制台。

您可以根据自己的需求修改此示例,例如更改要请求的网站URL,更改要查找的元素,或将输出保存到文件中。

Node.js 爬虫脚本示例:提取网页链接

原文地址: https://www.cveoy.top/t/topic/nruk 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录