首页 > 简单的php爬虫案例是什么呢?

简单的php爬虫案例是什么呢?

本人php新手,尤其是对php运用极差。
有没有来一段php爬虫的简单案例,激发新手对php热爱的欲望。
比如用php爬某个网站的数据库?
请问如何用php不同的方法和不一样的函数或者用正则比。用file还是什么的?


<?php
require dirname(__FILE__).'/simple_html_dom.php';
$html = file_get_html('http://php.net');
$news = array();
foreach($html->find('article.newsentry') as $article) {
    $item['time']    = trim($article->find('time',            0)->plaintext);
    $item['title']   = trim($article->find('h2.newstitle',    0)->plaintext);
    //$item['content'] = trim($article->find('div.newscontent', 0)->plaintext);
    $news[] = $item;
}
print_r($news);

比如上面就是用Simple HTML DOM这个PHP的DOM分析库采集php.net的首页新闻,可以很方便地像jQuery那样进行DOM操作,获取HTML里需要的数据.
http://simplehtmldom.sourceforge.net/manual.htm


能好好提问吗?
谁告诉你能爬别人网站的数据库?数据!=数据库.
获取HTML内容的方法可以用file_get_contents(),curl,fopen,fsockopen,etc

最简单的:获取首页内容:

echo file_get_contents('https://.com/');

HTML内容提取

可能你需要对页面内容进行提取,可以用正则表达式,但是不建议这么用,一旦网站改版很难维护,或者html内容不规则,

建议使用phpquery这样的DOM解析,有国人基于phpquery开发的querylist


https://github.com/search?utf8=%E2%9C%93&q=php+crawler

【热门文章】
【热门文章】