Today, Jerry noticed that after the revision of the brief book, the old search function by question title has disappeared, making it difficult for me to search my past posts. Therefore, I wanted to write a small program to download the titles and hyperlinks of my past articles.

The code is as follows:

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) {});Copy the code

Output after execution:

1042416@CTUN50947961A c:\Code\wechat\unittest$ node jianshuexport. js
titleRedesign SAP CRM One Order Redesign/ / www.jianshu.com/p/5c1d0319dc42title:, one of the most simple webSocket hello world demourl: https://www.jianshu. Com /p/ b820e0acc120Title: a very useful tool to open Windows CMD in multi-tab mode url: https://www.jianshu.com/p/3f8cc02eaa8etitle: web based On SAML in use On a concrete example of the url: https://www.jianshu.com/p/19344c23doaftitle: SAP CX Upscale Commerce: SAP new platform for the electric ShangYun url: https://www.jianshu.com/p/5d8352653e67title: TCP socket and web socket area to another
url: https:/ / www.jianshu.com/p/8a08f81d3efbtitle: when the Visual Studio code set breakpoints unverified breakpoint error handling url: https://www.jianshu.com/p/d004f4dc7826title: Fiori Fundamentals and SAP UI5 Web Componentsurl: https://www.jianshu.com/p/5389ca51fa44title: SAP front-end technology evolution profile
url: https://www.jianshu.com/p/140e07afd035
Copy the code

For more of Jerry’s original articles, please follow the public account “Wang Zixi “: