一般php采集代码能用的到,这里只是简单的实现代码。
小偷程序:
<?php
$title='脚本学堂';
$hello='jbxue.com!';
$file=file_get_contents('http://www.jbxue.com');
$file=iconv("gbk//IGNORE","utf-8",$file);
//echo $file;
echo strpos($file,'<title>');
echo substr($file,strpos($file,'<title>')+7,strpos($file,'</title>'-strpos($file,'<title>')+7));
$file=str_replace(array('百度一下','{hello}'),array($title,$hello), $file);
?> 小偷程序:
<?php
function _url($Date){
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, "$Date");
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$contents = curl_exec($ch);
curl_close($ch);
return $contents;
}
$url="http://www.jbxue.com";
$contents.=_url($url);
echo $contents; //输出内容
?>
本文介绍了一段使用PHP进行网页内容抓取的示例代码。通过file_get_contents及curl函数获取目标网站的内容,并利用字符串操作函数进行简单的内容替换与提取。此代码适用于初学者了解基本的网页抓取技巧。
118

被折叠的 条评论
为什么被折叠?



