"use strict"; let cheerio = require("cheerio"); let http = require("http"); let iconv = require("iconv-lite"); let mainUrl = "http://news.sina.com.cn/world/"; http.get(mainUrl, function(sres) { var chunks = []; sres.on(‘data‘, function(chunk) { chunks.push(chunk); }); sres.on(‘end‘, function() { var html = iconv.decode(Buffer.concat(chunks), ‘utf8‘); var $ = cheerio.load(html, {decodeEntities: false}); $(‘.content a‘).each(function (idx,element){ let ele = $(element); let title = ele.text(); title = !!title?title.trim():‘‘; let url = ele.attr(‘href‘); url = !!url?url.trim():‘‘; if(title.length>4 && url.length>0&&url.indexOf(‘javascript‘)===-1){ console.log(title+‘|‘+url); } }) }); });
原文地址:https://www.cnblogs.com/sheryee/p/8783446.html
时间: 2024-10-13 21:30:38