这篇文章首发在吹水小镇:http://blog.reetsee.com/archives/366
要在手机或者电脑看到更好的图片或代码欢迎到博文原地址。也欢迎到博文原地址批评指正。
??????????????????????????????
export LANG=en_US.UTF-8
<?phprequire_once('phpfetcher.php');class mycrawler extends Phpfetcher_Crawler_Default { public function handlePage($page) { //打印处当前页面的title $res = $page->sel('//title'); for ($i = 0; $i < count($res); ++$i) { echo $res[$i]->plaintext; echo "\n"; } }}$crawler = new mycrawler();$arrJobs = array( //任务的名字随便起,这里把名字叫qqnews //the key is the name of a job, here names it qqnews 'qqnews' => array( 'start_page' => 'http://news.qq.com/a/20140927/026557.htm', //起始网页 'link_rules' => array( /* * 所有在这里列出的正则规则,只要能匹配到超链接,那么那条爬虫就会爬到那条超链接 * Regex rules are listed here, the crawler will follow any hyperlinks once the regex matches */ ), //爬虫从开始页面算起,最多爬取的深度,设置为1表示只爬取起始页面 //Crawler's max following depth, 1 stands for only crawl the start page 'max_depth' => 1, ) , );//$crawler->setFetchJobs($arrJobs)->run(); 这一行的效果和下面两行的效果一样$crawler->setFetchJobs($arrJobs);$crawler->run();[root@reetsee demo]# php single_page.php 王思聪回应遭警方调查:带弓箭不犯法 我是绿箭侠_新闻_腾讯网
<!DOCTYPE html><html lang="zh-CN"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"></meta> <meta charset="gb2312"></meta> <title> 王思聪回应遭警方调查:带弓箭不犯法 我是绿箭侠_新闻_腾讯网 </title>
<?php//下面两行使得这个项目被下载下来后本文件能直接运行$demo_include_path = dirname(__FILE__) . '/../';set_include_path(get_include_path() . PATH_SEPARATOR . $demo_include_path);require_once('phpfetcher.php');class mycrawler extends Phpfetcher_Crawler_Default { public function handlePage($page) { //打印处当前页面的第1个h1标题内荣(下标从0开始) $strFirstH1 = trim($page->sel('//h1', 0)->plaintext); if (!empty($strFirstH1)) { echo $page->sel('//h1', 0)->plaintext; echo "\n"; } }}$crawler = new mycrawler();$arrJobs = array( //任务的名字随便起,这里把名字叫qqnews //the key is the name of a job, here names it qqnews 'qqnews' => array( 'start_page' => 'http://news.qq.com', //起始网页 'link_rules' => array( /* * 所有在这里列出的正则规则,只要能匹配到超链接,那么那条爬虫就会爬到那条超链接 * Regex rules are listed here, the crawler will follow any hyperlinks once the regex matches */ '#news\.qq\.com/a/\d+/\d+\.htm$#', ), //爬虫从开始页面算起,最多爬取的深度,设置为2表示爬取深度为1 //Crawler's max following depth, 1 stands for only crawl the start page 'max_depth' => 2, ) , );$crawler->setFetchJobs($arrJobs)->run(); //这一行的效果和下面两行的效果一样//$crawler->setFetchJobs($arrJobs);//$crawler->run();
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号