php抓取网页内容

function curl_file_get_contents($durl){
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $durl);
  curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_);
  curl_setopt($ch, CURLOPT_REFERER,_REFERER_);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  $r = curl_exec($ch);
  curl_close($ch);
  return $r;
}

//例1
$txt = file_get_contents('http://ju.taobao.com/tg/today_items.htm?spm=608.1000525.0.51&frontCatId=4000');
//$txt=curl_file_get_contents('http://ju.taobao.com/tg/today_items.htm?spm=608.1000525.0.51&frontCatId=4000');
$txt=mb_convert_encoding($txt,"UTF-8","GBK");

//$tpic	= '/<img width=\"285\" data-ks-lazyload=\"([^\"]+)\"\/>/isu';
$tpic	= '/<img width=\"285\" data-ks-lazyload=\"([^<>]+)\"\/>/isu';
$ttitle = '/<h3><a target=\"_blank\" title=\"([^<>]+)\" href/s';
//preg_match_all($tpic, $txt, $m);
//var_dump($m);

preg_match_all($tpic,$txt,$match1[]);
preg_match_all($ttitle,$txt,$match1[]);


for($i=0;$i<10;$i++){
	//echo $match1[1][1][$i].'<br>';
	echo '图片:<img src='.$match1[0][1][$i].'><br>';
	echo '标题'.$match1[1][1][$i].'<br>';
}



//例2

$contents = file_get_contents("http://video.baidu.com/top/");
$contents = iconv("gb2312", "utf-8",$contents);

$paiming = '/<span class=\"color-v6 sum\">(\d+)<\/span>/s';  // 排名
$title   = '/<span class=\"matter\" title=\"([^<>]+)\">/s';    // 标题
$url	 = '/<a statisic=\"name\"  href=\'([^<>]+)\' class=\"block\" target=\"_blank\" >/s';
$num     = '/<span class=\"color-v6 tr\">(\d+)<\/span>/s';       // 浏览量
preg_match_all($paiming,$contents,$match[]);
preg_match_all($title,$contents,$match[]);
preg_match_all($url,$contents,$match[]);
preg_match_all($num,$contents,$match[]);

//print_r($match);
echo '<table><tr><td>排名</td><td>电影名称</td><td>网址</td><td>点击量</td></tr>';
for($i=0;$i<10;$i++){
	echo '<tr><td>'.$match[0][1][$i].'</td>
			  <td>'.$match[1][1][$i].'</td>
			  <td><a href='.$match[2][1][$i].' target="_blank">'.$match[2][1][$i].'</a></td>
			  <td>'.$match[3][1][$i].'</td></tr>';
}
echo '</table>';
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值