使用 Node.js 爬取 GitChat 全站课程

GitChat 课程页面分析

课程列表页

GitChat 课程页面链接为:https://gitbook.cn/gitchat/columns,第一次加载完成加载了30条数据,之后通过滚动条滚动到页面底部懒加载的方式,调用相关方法,获取之后的数据。

2019-06-03_222554.png

查看浏览器控制台得知,每次请求数据调用的接口为:https://gitbook.cn/gitchat/columns?page=n&searchKey=&tag=,其中 n 为当前页面的页数, searchKey 为查询的关键词、 tag 为查询的标签,因为这里要爬取所有的课程数据,所以这里不需要 searchKeytag 参数。

当请求 https://gitbook.cn/gitchat/columns?page=1 时,也就是获取第一页数据,和页面初次加载(访问 https://gitbook.cn/gitchat/columns)获取到的数据是一样的,所以通过 https://gitbook.cn/gitchat/columns?page=n 不断请求接口,就可以获取到所有的课程列表数据(课程链接)

GitChat 源码中调用该接口的具体方法为:

    function loadMoreItems() {
   
        currentPage += 1;
        $.ajax({
   
            url: "/gitchat/columns?page=" + currentPage + '&searchKey=&tag=',
            type: "GET",
            contentType: "application/json; charset=utf-8",
            success: function(data, status) {
   
                if (status == 'success' && data.code == 0) {
   
                    $('#colum-items #list_view').append(data.data);
                    console.log(data)
                    if (data.isEnd) {
   
                        if ('特价' == '') {
   
                            currentPage--;
                            $.ajax({
   
                                url: "/gitchat/columns?page=" + currentPage + '&searchKey=',
                                type: "GET",
                                contentType: "application/json; charset=utf-8",
                                success: function(data, status) {
   
                                    if (status == 'success' && data.code == 0) {
   
                                        $('#colum-items #list_view').append(data.data);
                                        if (data.isEnd) {
   
                                            scene.destroy(true);
                                            $("#loader").removeClass("active");
                                            $("#loader").hide();
                                            $("#noMoreToLoad").show();
                                        } else {
   
                                            scene.update();
                                            $("#loader").removeClass("active");
                                        }
                                    }
                                }
                            })
                        } else {
   
                            scene.destroy(true);
                            $("#loader").removeClass("active");
                            $("#loader").hide();
                            $("#noMoreToLoad").show();
                        }

                    } else {
   
                        scene.update();
                        $("#loader").removeClass("active");
                    }
                    $('.banner').lazyload({
   
                        threshold: 100,
                        effect: "fadeIn"
                    })
                }
            }
        });
    }

response.isEndtrue 时,表示结束了,这是最后一页数据了,这时就不再需要调用接口去请求了。

课程详情页

从课程列表页点击任意一条课程链接,会进入课程详情页,如:https://gitbook.cn/gitchat/column/5c0e149eedba1b683458fd5f,当前我的是超级会员,进入课程详情页后,页面右上角显示两个按钮:会员免费学和免费试读,点击会员免费学后触发方法来订阅该课程:

function submitVIPColomnOrder(o, r) {
   
    var i = $("#payColmunBtn")
      , a = i.html();
    i.addClass("link-not-active"),
    i.html("预订中...");
    var e = encodeURI(window.location.href);
    $.ajax({
   
        url: "/m/mazi/vip/order/column",
        type: "POST",
        data: JSON.stringify({
   
            columnId: o,
            requestUrl: e
        }),
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function(e, t) {
   
            if ("success" == t && 0 == e.code && "success" == e.message)
                window.location.href = "/m/mazi/comp/column?columnId=" + o + "&refresh=true&newBuyer=true";
        
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值