我们可以利用这个HTML分析器,项目地址: https://github.com/samacs/simple_html_dom
今天,我们准备抓取豆瓣的《艾伦•图灵传》,网址:http://book.douban.com/subject/11541213/?from=tag
通过HTML分析器:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php // example of how to use basic selector to retrieve HTML contents include('../simple_html_dom.php'); // get DOM from URL or file $html = file_get_html('http://book.douban.com/subject/11541213/?from=tag'); $title = $html->find('head title', 0); echo $title->plaintext; $html->clear(); unset($html); |
用了分析器是不是很方便,初学者使用分析器可以很快收集一些图书价格走势信息。不需要使用晦涩难懂的正则表达式,并且开发效率会事半功倍。