Can you provide examples of parsing HTML?

后端 未结 29 2737
走了就别回头了
走了就别回头了 2020-11-22 13:49

How do you parse HTML with a variety of languages and parsing libraries?


When answering:

Individual comments will be linked to in answers to questions

29条回答
  •  庸人自扰
    2020-11-22 14:22

    Language: JavaScript/Node.js

    Library: Request and Cheerio

    var request = require('request');
    var cheerio = require('cheerio');
    
    var url = "https://news.ycombinator.com/";
    request(url, function (error, response, html) {
        if (!error && response.statusCode == 200) {
            var $ = cheerio.load(html);
            var anchorTags = $('a');
    
            anchorTags.each(function(i,element){
                console.log(element["attribs"]["href"]);
            });
        }
    });
    

    Request library downloads the html document and Cheerio lets you use jquery css selectors to target the html document.

提交回复
热议问题