小小爬虫

news/2024/7/19 12:30:50 标签: 爬虫, 数据库
const request=require('request');
const cheerio=require('cheerio');
(function () {
    //页面信息==》简介相关
    var getInfo=function (i) {
        var url='http://www.cnblogs.com/flyings/default.html?page='+i;
        request(url,function (err,res,body){
            let $ = cheerio.load(body);
            for(let i=0; i<$('.postTitle').length; i++){
                let href=$('.postTitle').eq(i).find('a').attr('href');
                let bid=/\d*.html/.exec(href)[0].replace('.html','');
                let title=$('.postTitle').eq(i).find('a').text();
                let desc=$('.postCon').eq(i).find('.c_b_p_desc').text().replace(/阅读全文/ig,"").replace(/摘要:/ig,"");
                let blog={bid:bid,title:title, desc:desc }

                console.log(blog)


                // 存到数据库
                // let param={
                //     where:{bid:blog.bid},
                //     data:blog,
                //     option:{upsert:true}
                // }
                //blogModel.saveOrUpdate(param, function (error) {});


                //根据pid爬出详情数据
                getInforDetail(bid)
            }

        })
    }

    //页面信息==》详情相关
    var getInforDetail=function (j) {
        var url='http://www.cnblogs.com/flyings/p/'+j+'.html';
        request(url,function (err,res,body){
            let $ = cheerio.load(body);
            let bid=j;
            let title=$('.postTitle').find('a').text();
            let content=$('#cnblogs_post_body').html();
            let blogDetail={bid:bid,title:title,content:content}
            console.log(blogDetail)

            // 存到数据库
            // let param={
            //     where:{bid:blogDetail.bid},
            //     data:blogDetail,
            //     option:{upsert:true}
            // }
            // blogDetailModel.saveOrUpdate(param, function (error) {});
        })
    }


    //获取自己博客文章总页数,并开始爬数据
    var getInit=function () {
        request('http://www.cnblogs.com/flyings/default.html?page=2',function (err,res,body){
            console.log(123)
            let $ = cheerio.load(body);
            let str= $('.pager').eq(1).text();
            let pages=/共\d*页/.exec(str)[0].replace(/[^0-9]/ig,"");
            for(let i=0; i<pages; i++){
                getInfo(i)
            }
        })
    }

    getInit()


})()

转载于:https://www.cnblogs.com/dshvv/p/8016639.html


http://www.niftyadmin.cn/n/1629784.html

相关文章

CMake中执行shell命令之execute_process、add_custom_target和add_custom_command

背景 以下情况可能需要在CMake中执行shell脚本&#xff1a; cmake未提供的功能而实际构建中又需要时&#xff0c;如获取Linux发行版本项目构建时需要执行脚本才能完成&#xff0c;如boost构建过程 有的需要shell脚本的返回值&#xff0c;而有的不需要&#xff0c;这个关系不大…

【C#】Entity Framework 增删改查和事务操作

1、增加对象   DbEntity db new DbEntity();//创建对象实体&#xff0c;注意&#xff0c;这里需要对所有属性进行赋值&#xff08;除了自动增长主键外&#xff09;&#xff0c;如果不赋值&#xff0c;则会数据库中会被设置为NULL&#xff08;注意是否可空&#xff09;var us…

CMake识别操作系统平台及Linux发行版本是ubuntu/centos

识别操作系统平台 以下代码可以识别 Windows、Linux、Macos三种类型的操作系统平台&#xff1a; IF (CMAKE_SYSTEM_NAME MATCHES "Linux") MESSAGE(STATUS "current platform: Linux ") ELSEIF (CMAKE_SYSTEM_NAME MATCHES "Windows") MESSAGE…

Buffer对象的总结

1.首先记录下php关于字符串长度的统计 <?php echo strlen(0123456789);//10 echo <br>; echo strlen(一二三四五六七八九十);//30 echo <br>; echo mb_strlen(一二三四五六七八九十,utf-8);//10 2.Buffer对象为16进制的两位数&#xff0c;即FF 0-225&#xff1…

嵌入式C语言中的volatile关键字

原文地址http://www.cnblogs.com/armlinux/archive/2010/09/14/2396918.html/*********以下是转载《16道嵌入式C语言面试题中的一题》***********/关键字volatile有什么含意 并给出三个不同的例子。 一个定义为volatile的变量是说这变量可能会被意想不到地改变&#xff0c;这样…

几款代码混淆器(DotFuscator, .NET Reactor, xenocode)

http://kenchell.blog.163.com/blog/static/26088309201311810523681 http://blog.csdn.net/yanpingsoft/article/details/7997212

DataFrame保存hdf5文件报错:TypeError:object of type ‘int‘ has no len()4

背景 最近需要把数据从csv文件中读取出来&#xff0c;然后写入hdf5中。 使用DataFrame读取csv&#xff0c;然后调用df的to_hdf方法直接写入hdf5文件。 我使用的是table模式&#xff0c;示例代码如下&#xff1a; import pandas as pd import numpy as npdays [2021-01-01, …

[转][darkbaby]任天堂传——失落的泰坦王朝(上)

前言:曾经一再的询问自我;是否真的完全了解任天堂这个游戏老铺的真实本质?或许从来就没有人能够了解&#xff0c;世间已经有太多的真相被埋没在谎言和臆测之中。作为 一个十多年游龄的老玩家&#xff0c;亲眼目睹了任天堂从如日中天慢慢走向落落寡合&#xff0c;或许终有一天&…