目录
GitChat 课程页面分析
课程列表页
GitChat 课程页面链接为:https://gitbook.cn/gitchat/columns,第一次加载完成加载了30条数据,之后通过滚动条滚动到页面底部懒加载的方式,调用相关方法,获取之后的数据。
查看浏览器控制台得知,每次请求数据调用的接口为:https://gitbook.cn/gitchat/columns?page=n&searchKey=&tag=,其中 n
为当前页面的页数, searchKey
为查询的关键词、 tag
为查询的标签,因为这里要爬取所有的课程数据,所以这里不需要 searchKey
和 tag
参数。
当请求 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.isEnd
为 true
时,表示结束了,这是最后一页数据了,这时就不再需要调用接口去请求了。
课程详情页
从课程列表页点击任意一条课程链接,会进入课程详情页,如: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";