今天Jerry发现简书改版后,以前根据问题标题进行搜索的功能消失了,这样我搜索自己过去发表的文章就不太方便。因此,我想写个小程序,把自己过去发表的文章的标题和超链接都下载到本地。
代码如下:
var request = require('request');
var jsdom = require("jsdom");
var JSDOM = jsdom.JSDOM;
const PREFIX = "https://www.jianshu.com";
/* a given article: https://www.jianshu.com/p/963cd23fb092
value got from API: /p/5c1d0319dc42
*/
var url = "https://www.jianshu.com/u/99b8712e8850?order_by=shared_at&page=2";
var pageOptions = {
url: url,
method: "GET",
headers: {
"Accept": "text/html"
}
};
function getArticles() {
return new Promise(function(resolve,reject){
var requestC = request.defaults({jar: true});
requestC(pageOptions,function(error,response,body){
var document = new JSDOM(body).window.document;
var content = document.getElementsByTagName("li");
for( var i =0; i < content.length; i++){
var li = content[i];
var children = li.childNodes;
for( var j = 0; j < children.length; j++){
var eachChild = children[j];
if( eachChild.nodeName == "DIV"){
var grandChild = eachChild.childNodes;
for( var k = 0; k < grandChild.length; k++){
var grand = grandChild[k];
if( grand.nodeName == "A"){
var fragment = grand.getAttribute("href");
if( fragment.indexOf("/p") < 0)
continue;
console.log("title: " + grand.text);
var wholeURL = PREFIX + fragment;
console.log("url: " + wholeURL);
}
}
}
}
}
});
});
}
getArticles().then(function(token) {
});
执行后的输出:
1042416@CTUN50947961A c:\Code\wechat\unittest$ node jianshuexport. js
title:我做SAP CRM One Order redesign的一些心得体会url: https://www.jianshu.com/p/5c1d0319dc42title:一个最简单的webSocket hello world demourl: https://www.jianshu. com/p/b820e0acc120title:推荐一个非常好用的以多tab标签方式打开windows CMD的工具url: https://www.jianshu.com/p/3f8cc02eaa8etitle:基于SAML的web Single Sign On的一个具体例子url: https://www.jianshu.com/p/19344c23doaftitle: SAP CX Upscale Commerce : SAP全新推出的电商云平台url: https://www.jianshu.com/p/5d8352653e67title: TCP socket和web socket的区另
url: https://www.jianshu.com/p/8a08f81d3efbtitle: Visual Studio code设置断点时出现unverified breakpoint的错误处理方式url: https://www.jianshu.com/p/d004f4dc7826title: Fiori Fundamentals和SAP UI5 Web Componentsurl: https://www.jianshu.com/p/5389ca51fa44title: SAP前端技术的演化史简介
url: https://www.jianshu.com/p/140e07afd035