碧海潮声大学生网

标题: 关于php抓取激动800*800大图以及商品价格和名称 [打印本页]

作者: 大学虫    时间: 2014-9-24 00:32
标题: 关于php抓取激动800*800大图以及商品价格和名称

  • function getjd($path,$pagenum){
  • $url=$path;
  • $pages=$pagenum;
  • $arr=explode('-',$url);
  • for($i=1;$i<=$pages;$i++){//$pages为分页的数量
  • $arr[14]=$i;//url中最后一个1所在的位置
  • $url=implode('-',$arr);
  • $curl = curl_init ();//初始化curl
  • curl_setopt ( $curl, CURLOPT_URL, $url );
  • curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
  • curl_setopt($curl, CURLOPT_HEADER, 1);
  • curl_setopt ( $curl, CURLOPT_POST, 1 );
  • curl_setopt ( $curl, CURLOPT_POSTFIELDS, $data );
  • $html = curl_exec ( $curl );//获得页面内容
  • preg_match_all ( "/<script type=\"text\/javascript\">(.*?)<\/script>/", $html, $prices );
  • //var_dump($prices);
  • $priceArray = explode ( "\"", $prices [1] [1] );//根据不同的页面不同使用var_dump($prices);找到这个文件在做位置 {"key":"0947B44DB97B32EF6ABF5F437018B33D8397430A","skuids":"
  • $skuids = str_replace(",",",J_",$priceArray [7]);
  • $priceURL = "http://p.3.cn/prices/mgets?skuIds=J_{$skuids}&&type=1";
  • $priceString = file_get_contents($priceURL);
  • $json = json_decode($priceString);
  • //获取出商品名称及200*200图像路径输出(更多操作请自行跟去需要添加:如写入数据库)
  • preg_match_all ( "/<img .*? alt='(.*)' (data-lazyload|src)='(.*?)' .*\/>/", $html, $macthes );
  • foreach($macthes[1] as $key => $alt){
  • $proname = $alt;
  • $image = basename($macthes[3][$key]);
  • $price = $json[$key] -> p;
  • echo "<p>name:{$proname},price:{$price},image:{$image}</p>";
  • }
  • preg_match_all ( "/href='(.*?)'><img width='220'/", $html, $match1 );
  • var_dump($match1);
  • $detailUrl=$match1[1];//获得商品详情页面url
  • foreach($detailUrl as $val){
  • //$val=preg_replace("/'/",'',$val);
  • $detailHtml=file_get_contents($val);
  • preg_match_all ( '/src="(.*?)" width="50"/', $detailHtml, $match2 );
  • $n5=$match2[1];//获得n5图片
  • foreach($n5 as $img){
  • $img=preg_replace("/n5/","n0",$img);
  • /*
  • //京东图片服务器很有规律仔细一研究就能找到
  • 诸如:
  • img12.360buyimg.com后边的n5为50*50格式图片只需改为n0就能得到800*800格式图片,
  • 其他格式图片分别改为1、2、3、4、5...即可
  • */
  • //把文件写入当前文件夹的n0文件夹中
  • file_put_contents('./n0/'.$k++.basename($img),file_get_contents($img));
  • }
  • $j=0;
  • echo "page".$j++."<br>";
  • }
  • }
  • }

[color=rgb(51, 102, 153) !important]复制代码



实例化运行试试: $url='http://list.jd.com/670-671-6864-0-0-0-0-0-0-0-1-1-1-1-1-72-4137-0.html';getjd($url,6);关于curl我就不多说,curl可以做很多事,在获取internet文件时要比file_get_contents稳定,它有100多个配置参数,够喝一壶的。
注意:url必须是这种数字递增格式的,不然不能对全部页面进行遍历。

运行php文件,然后等京东的图片自己跑过来(注意更改一下php.ini 的max_execution_time选项,默认php文件执行时间是30秒,如果你网速慢现在文件多的话会中断





欢迎光临 碧海潮声大学生网 (http://www.zjoubbs.com/) Powered by Discuz! X3.2