Can you provide examples of parsing HTML?

后端 未结 29 2650
走了就别回头了
走了就别回头了 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 14:27

    Using phantomjs, save this file as extract-links.js:

    var page = new WebPage(),
        url = 'http://www.udacity.com';
    
    page.open(url, function (status) {
        if (status !== 'success') {
            console.log('Unable to access network');
        } else {
            var results = page.evaluate(function() {
                var list = document.querySelectorAll('a'), links = [], i;
                for (i = 0; i < list.length; i++) {
                    links.push(list[i].href);
                }
                return links;
            });
            console.log(results.join('\n'));
        }
        phantom.exit();
    });
    

    run:

    $ ../path/to/bin/phantomjs extract-links.js
    

提交回复
热议问题