Node request、cheerio

14 篇文章 0 订阅
7 篇文章 0 订阅

request

用来发送请求的库

  • 发送request
request('http://www.baidu.com', function (error, response, body) {
    console.log(body); //body.com/index.html 的内容
});
//图片下载
const file = "https://upload.jianshu.io/users/upload_avatars/7232100/3ac5a4a7-eb45-4bff-8413-3c0b4684ad6d?imageMogr2/auto-orient/strip|imageView2/1/w/120/h/120";
request(file).pipe(fs.createWriteStream('li.png')); //下载图片
  • 设置 header
request({
    "url": 'https://www.baidu.com',
    "headers": {
        'User-Agent': 'request'
    }
},(err,res,body)=>{
    if(err){
        throw err
    }
    console.log(res);
});
  • cookie
rp({
    "url": 'https://www.baidu.com',
    "headers": {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
        'Cookie':'abc=123'
    },
})
.then(res=>{
    console.log('res');
    const result = /q857637472/i.exec(res);
    if(result&&result[0]){
        console.log(result[0]);        
    }
})
.catch(err=>{
    console.log(err);
})
  • promise 异步化
const res= rp('http://www.baidu.com')
    .then(function (htmlString) {
        console.log('2222');
        console.log(htmlString);
        // Process html...
    })
    .catch(function (err) {
        console.log('err');
        // Crawling failed...
    });
  • 乱码设置
const iconv = require("iconv-lite");
request.get({
    url : url ,
    encoding : null //让body 直接是buffer
}, response);

var response = function (err, response, body) {
    //返回的body 直接就是buffer 了...
    var buf =  iconv.decode(body, 'gb2312');
    
}

cheeio

用来解析HTML的库

  • 加载html
const html = `<ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
    </ul>`;

const $ = cheerio.load(html);

const li2 = $('li:nth-child(2)');
console.log(li2); //一个dom 节点 和react Vdom 形式一致
  • 解析
const text = li2.text(); //获取 dom 内容
console.log(text);
const id = li2.attr('data-id');
console.log(id);
  • 遍历
const lis = $('ul>li');
if(lis&&lis.length){
    lis.each((i,el)=>{
        console.log(`index:${i}`);
        console.log(`el:${el}`);
    });
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值