• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP采摘代码

php 搞代码 3年前 (2022-01-23) 28次浏览 已收录 0个评论

PHP采集代码
采集http://www.01job.cn/asp/itjob.asp该页面中职位列表头三条的记录
看了很多的PHP采集教程了 还是不会写 所以这里请教了….
以下是我的代码,运行时了错:
<?
include(“function.php”);
$url=”http://www.01job.cn/asp/itjob.asp&#8221;;
$ft[“title”][“begin”]=”

“;
$ft[“title”][“end”]=”

“;
$rs=pick($url,$ft,$th);
echo $rs[“title”];
echo “
内容:”.$rs[“content”];
?>
function.php
<?php
//获取网页内容
Function fetch_urlpage_contents($url){
$c=file_get_contents($url);
return $c;
}

//获取匹配内容
Function fetch_match_contents($begin,$end,$c)
{
$begin=change_match_string($begin);
$end=change_match_string($end);
if(@preg_match(“/{$begin}(.*?){$end}/i”,$c,$rs))
{return $rs[1];}
else {return “”;}
}

//转义正则表达式字符串
Function change_match_string($str){
//注意,以下只是简单转义
$old=array(“/”,”$”);
$new=array(“\/”,”\$”);
$str=str_replace($old,$new,$str);
return $str;
}

//采集网页
Function pick($url,$ft,$th)
{
$c=fetch_urlpage_contents($url);
foreach($ft as $key => $value)
{
$rs[$key]=fetch_match_contents($value[“begin”],$value[“end”],$c);
if(is_array($th[$key]))
{ foreach($th[$key] as $old => $new)
{
$rs[$key]=str_replace($old,$new,$rs[$key]);
}
}
}
return $rs;
}

?>

——解决方案——————–
<?
//获取网页内容
Function fetch_urlpage_contents($url){
for($i=0;$i<10;$i++)
{
$c=@file_get_contents($url);
if(trim($c) != “”)break;
}
// print($c);
return $c;
}

//获取匹配内容
Function fetch_match_contents($begin,$end,$c)
{
$beginPos = strpos($c,$begin);
$endPos = strpos($c,$end);
if($beginPos > 0 && $endPos > 0 && $endPos > $beginPos)
{
$result = substr($c,$beginPos+strlen($begin),$endPos – $beginPos-strlen($begin));
return $result;
}
else
{
return “”;
}
}

//采集网页
Function pick($url,$ft,$th)
{
$c=fetch_urlpage_contents($url);
foreach($ft as $key => $value)
{
$rs[$key]=fetch_match_contents($value[“begin”],$value[“end”],$c);
if(is_array($th[$key]))
{ foreach($th[$key] as $old => $new)
{
$rs[$key]=str_replace($old,$new,$rs[$key]);
}
}
}
return $rs;@本文9来源gao($daima.com搞@代@#码8网^搞代gaodaima码
}
?>

caiji

<meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″>

<body>
<?

$url=”http://www.01job.cn/asp/itjob.asp&#8221;;
$ft[“title”][“begin”]=”“; <br />$ft[“title”][“end”]=”“;
$rs=pick($url,$ft,$th);
print_r($rs);
?>

——解决方案——————–
写个例子。。

PHP code

$content = file_get_contents('http://www.01job.cn/asp/itjob.asp');preg_match_all('/(.*)<\/a>/',$content,$arr);print_r($arr); 


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP采摘代码

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址