<?php
$filename="http://www.edeng.cn/index.html"; //设定开始抓取的网页地址
$content=file_get_contents($filename); //获取html内容
$filename = 'index1.htm'; //取名字
$handle=fopen($filename,'w');//打开文件并确认可写
fwrite($handle,$content);
echo "<script>alert('成功地写入到文件$filename');</script>";
?>
附带fopen操作说明:
fopen(PHP 3, PHP 4 ) fopen-- 打开文件或者 URL说明resource fopen( string filename, string mode [, int use_include_path [, resource zcontext]])fopen()将filename指定的名字资源绑定到一个流上。如果filename是 "scheme://..." 的格式,则被当成一个 URL,PHP 将搜索协议处理器(也被称为封装协议)来处理此模式。如果该协议尚未注册封装协议,PHP 将发出一条消息来帮助检查脚本中潜在的问题并将filename当成一个普通的文件名继续执行下去。 如果 PHP 认为filename指定的是一个本地文件,将尝试在该文件上打开一个流。该文件必须是 PHP 可以访问的,因此需要确认文件访问权限允许该访问。如果激活了安全模式或者open_basedir则会应用进一步的限制。 如果 PHP 认为filename指定的是一个已注册的协议,而该协议被注册为一个网络 URL,PHP 将检查并确认allow_url_fopen已被激活。如果关闭了,PHP 将发出一个警告,而fopen的调用则失败。
mode参数指定了所要求到该流的访问类型。可以是以下: 表格 1.fopen()中的mode的可能值列表
如果也需要在include_path中搜寻文件的话,可以将可选的第三个参数use_include_path设为 '1' 或TRUE。 如果打开失败,本函数返回FALSE。
如果在用服务器模块版本的 PHP 时在打开和写入文件上遇到问题,记住要确保所使用的文件是服务器进程所能够访问的。 在 Windows 平台上,要小心转义文件路径中的每个反斜线,或者用斜线。
|
fwirte例子:
例子 1. 一个简单的fwrite范例
<?php |
========================================================
基于PHP抓取MSN SPACE的RSS信息
http://hi.baidu.com/meloidea/blog/item/db1d18306fef1d9da9018e92.html
RSS(也叫聚合内容,Really Simple Syndication)是一种描述和同步网站内容的格式,是目前使用最广泛的XML应用,可以有效的实现网络内容信息资源的共享。
基于PHP有很多现成的RSS聚合类,下面以lastRSS为例介绍一下如何去抓取微软的MSN SPACE日志信息。
首先需要下载lastRSS类,地址是:http://lastrss.oslab.net/。下载下来解压,就一个文件lastRSS.php,非常简单,使用代码如下,比如要抓取RSS种子是'http://meloidea.spaces.live.com/feed.rss',代码如下:
<?php
//指定PHP文件输出的编码,避免乱码
header('content-type:text/html;charset=utf-8');
// 加载 lastRSS 类
include "lastRSS.php";
// 创建 lastRSS 对象
$rss = new lastRSS;
//设置缓存目录
$rss->cache_dir = './temp';
//缓存保留时间1200秒
$rss->cache_time = 1200;
// 加载RSS源
if ($rs = $rss->get('http://meloidea.spaces.live.com/feed.rss')) {
// 显示网站标识图
if ($rs[image_url] != '') {
echo "<a href=/"$rs[image_link]/"><img src=/"$rs[image_url]/" alt=/"$rs[image_title]/" vspace=/"1/" border=/"0/" /></a><br />/n";
}
//显示网站标题
echo "<big><b><a href=/"$rs[link]/">$rs[title]</a></b></big><br />/n";
//显示网站描述
echo "$rs[description]<br />/n";
//显示最新的日志 (标题,链接和描述)
echo "<div align=left>";
foreach($rs['items'] as $item) {
echo "<li><a href=/"$item[link]/">".$item[title]."</a><br />".html_entity_decode($item['description'])."</li>";
}
echo "</div>";
}
else {
echo "错误: 找不到RSS源文件信息/n";
}
?>
注意:MSN SPACE需要使用html_entity_decode($item['description'],否则日志内容将显示成HTML的代码
当然除了lastRSS还有magpierss和gregarius也可以实现RSS的聚合
========================================================
PHP抓取远程网站数据的代码
http://hi.baidu.com/kaxi/blog/item/071c513dd94477ea3c6d97da.html
现在可能还有很多程序爱好者都会遇到同样的疑问,就是要如何像搜索引擎那样去抓取别人网站的HTML代码,然后把代码收集整理成为自己有用的数据!今天就等我介绍一些简单例子吧.
Ⅰ.抓取远程网页标题的例子:
以下是代码片段: |
Ⅱ.抓取远程网页HTML代码的例子:
以下是代码片段: |
以上两个代码片段都直接Copy回去运行就知道效果了,上面的例子只是抓取网页数据的雏形,要使其更适合自己的使用,情况有各异.所以,在此各位程序爱好者自己好好研究一下吧.
这篇文章介绍了两个比较基础的例子
========================================================
用PHP抓取百度空间的文章标题、内容及分类列表
http://hi.baidu.com/antsnet/blog/item/7efb5043387a8b179213c636.html
<!--power by antsnet.net-->
获取分类
function getSort($url = 'http://hi.baidu.com/antsnet')
{
$contents = file_get_contents($url);
if($contents)
{
//$contents = preg_replace("/[/r/n|/n]/", "", $contents);
$exp_match = "/<div class=/"item/"><a href=/"//antsnet//blog//category//(.*)/" title=/";
preg_match_all($exp_match, $contents, $match);
return $match[1];
}
}
获取文章列表及描述
function getDid($url){
if(stristr($url,"http://")==false){
$url="http://";
}
$exp_domain="/^http:////(.*)[/.com|/.cn|/.org|/.com/.cn|/.net/.cn|/.org/.cn]{1}///";//获取DOMAIN
$exp_header="/<//head>|<//HEAD>/";
preg_match($exp_domain,$url,$match);
$DOMAIN="http://hi.baidu.com";
$div_exp="/<div(.*)>(.*)(/r/n|/n)(.*)<//div>/";
$div_page="/<div(.*)id=/"page/">(.*)<//div>/";
$CONTENTS=@file_get_contents($url);
if(!$CONTENTS){
die("This url does not exit.");
}
preg_match_all($div_exp,$CONTENTS,$match);
preg_match_all($div_page,$CONTENTS,$myPage);
foreach($match[0] as $m){
if(stristr($m,"class=/"tit/"")!=false){
$m=str_replace("/antsnet","?act=article&path=/antsnet",$m);
//$m=str_replace("a href=","a target=/"_blank/" href=",$m);
//$m=str_replace("title=","target=/"_blank/" title=",$m);
$title[]=$m;
}
if(stristr($m,"class=/"item/"")){
$m=str_replace("/antsnet","?act=article&?path=antsnet",$m);
//$m=str_replace("a href=","a target=/"_blank/" href=",$m);
//$m=str_replace("title=","target=/"_blank/" title=",$m);
$sort[]=$m;
}
}
if(is_array($myPage)){
$page=str_replace("/antsnet","?act=Page&index=/antsnet",$myPage[0][0]);
}
@array_shift($title);
if(sizeof($title)==0){
header("Location: index.php");
exit();
}
$return["title"]=$title;
$return["sort"]=$sort;
$return["page"]=$page;
return $return;
}
获取文章内容
function getArticleContents($url = 'http://hi.baidu.com/antsnet/blog/item/f1fefbdc5df36aa4cc1166d8.html')
{
$contents = preg_replace("/[/r/n|/n]/", "", file_get_contents($url));
$exp_title = "/<title>(.*)<//title>/";
$exp_match = "/<table style=/"table-layout:fixed/">(.*)<//tr><//table><br>/";
preg_match_all($exp_match, $contents, $match);
preg_match_all($exp_title, $contents, $titles);
return '<p><span style="color:red;font-size:15px;">' . str_replace('_Antsnet.net', '', $titles[1][0]) . ' [snatch at : '.date('Y-m-d H:i:s').']</span><!--<a href="javascript.:history.go(-1);">[返回]</a>--></p>' . $match[0][0];
}
========================================================
php抓取蜘蛛
http://hi.baidu.com/piea/blog/item/ff82962f2c6b3f3e1e308903.html
搜索引擎的蜘蛛访问网站是通过远程抓取页面来进行的,我们不能使用JS代码来取得蜘蛛的Agent信息,但是我们可以通过image标签,这样我们就可以得到蜘蛛的agent资料了,通过对agent资料的分析,就可以确定蜘蛛的种类、性别等因素,我们在通过数据库或者文本来记录就可以进行统计了。
下面是我的程序和源代码:
数据库结构:
#
# 表的结构`naps_stats_bot`
#
CREATE TABLE `naps_stats_bot` (
`botid` int(10) unsigned NOT NULL auto_increment,
`botname` varchar(100) NOT NULL default '',
`botagent` varchar(200) NOT NULL default '',
`bottag` varchar(100) NOT NULL default '',
`botcount` int(11) NOT NULL default '0',
`botlast` datetime NOT NULL default '0000-00-00 00:00:00',
`botlasturl` varchar(250) NOT NULL default '',
UNIQUE KEY `botid` (`botid`),
KEY `botname` (`botname`)
) TYPE=MyISAM AUTO_INCREMENT=9 ;
#
# 导出表中的数据 `naps_stats_bot`
#
INSERT INTO `naps_stats_bot` VALUES (1, 'Googlebot', 'Googlebot/2.X(+http://www.googlebot.com/bot.html)', 'googlebot', 0, '0000-00-00 00:00:00','');
INSERT INTO `naps_stats_bot` VALUES (2, 'MSNbot', 'MSNBOT/0.1(http://search.msn.com/msnbot.htm)', 'msnbot', 0, '0000-00-00 00:00:00', '');
INSERT INTO `naps_stats_bot` VALUES (3, 'Inktomi Slurp', 'Slurp/2.0', 'slurp',0, '0000-00-00 00:00:00', '');
INSERT INTO `naps_stats_bot` VALUES (4, 'Baiduspider','Baiduspider+(+http://www.baidu.com/search/spider.htm)', 'baiduspider', 0,'0000-00-00 00:00:00', '');
INSERT INTO `naps_stats_bot` VALUES (5, 'Yahoobot','Mozilla/5.0+(compatible;+Yahoo!+Slurp;+http://help.yahoo.com/help/us/ysearch/slurp)','slurp', 0, '0000-00-00 00:00:00', '');
INSERT INTO `naps_stats_bot` VALUES (6, 'Sohubot', 'sohu-search','sohu-search', 0, '0000-00-00 00:00:00', '');
INSERT INTO `naps_stats_bot` VALUES (7, 'Lycos', 'Lycos/x.x', 'lycos', 0,'0000-00-00 00:00:00', '');
INSERT INTO `naps_stats_bot` VALUES (8, 'Robozilla', 'Robozilla/1.0','robozilla', 0, '0000-00-00 00:00:00', '');
PHP程序:
[Copy to clipboard]
CODE:
<?PHP
/***************************************************************************
* NAPS -- Network Article Publish System
* ----------------------------------------------
* bot.php
* -------------------
* begin : 2004-08-15
* copyright :(C) 2004 week9
*
***************************************************************************/
/***************************************************************************
*
* This program is free software; you can redistribute it and/ormodify
* it under the terms of the GNU General Public License as publishedby
* the Free Software Foundation; either version 2 of the License.
*
***************************************************************************/
/***************************************************************************
*
* NAPS产品是自由软件。你可以且必须根据《GNU GPL-GNU通用公共许可证》的相关规定
* 复制、修改及分发NAPS产品。任何以NAPS产品为基础的衍生发行版未必须经过飘飘的授权。
*
***************************************************************************/
error_reporting(E_ALL & ~E_NOTICE);
function get_naps_bot()
{
$useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
if (strpos($useragent, 'googlebot')!== false){
return 'Googlebot';
}
if (strpos($useragent, 'msnbot') !==false){
return 'MSNbot';
}
if (strpos($useragent, 'slurp') !==false){
return 'Yahoobot';
}
if (strpos($useragent,'baiduspider') !== false){
return 'Baiduspider';
}
if (strpos($useragent,'sohu-search') !== false){
return 'Sohubot';
}
if (strpos($useragent, 'lycos') !==false){
return 'Lycos';
}
if (strpos($useragent, 'robozilla')!== false){
return 'Robozilla';
}
return false;
}
$tlc_thispage = addslashes($_SERVER['HTTP_USER_AGENT']);
//添加蜘蛛的抓取记录
$searchbot = get_naps_bot();
if ($searchbot) {
$DB_naps->query("UPDATEnaps_stats_bot SET botcount=botcount+1, botlast=NOW(),botlasturl='$tlc_thispage' WHERE botname='$searchbot'");
}
?>
引用方法:
CODE:
<img src="./bot.php"width="0" height="0">
类似的浏览器和用户群也可分类。
稍微改变一下,就是一个简易版的 网站访问日志 程序了。
========================================================
相关阅读:
- 小巧的Mysql数据库备份脚本(PHP) (黄河, 2009-1-07)
- PHP抓取网页内容汇总 (黄河, 2009-1-07)
- PHP抓取网页内容汇总2 (黄河, 2009-1-07)