<?php set_time_limit(0); date_default_timezone_set("Asia/shanghai"); //自定义抓取图片地址 $url = 'http://item.jd.com/937680.html'; $ip_arr = get_ips(); $ip = trim(get_rand_ip($ip_arr)); //随机ip $content = get_content_by_url($url, $ip); //获取标题 preg_match("/<div id=\"name\">[\s]*<h1>(.*?)<\/h1>/i", $content, $match_title); if(isset($match_title[1]) && $match_title[1]){ $title = $match_title[1]; echo mb_convert_encoding('标题为:', 'GBK', 'UTF-8') . $title . '<br />'; }else{ echo mb_convert_encoding('没有获取到标题,程序终止:', 'GBK', 'UTF-8'); exit; } //获取价格和图片 preg_match("/\/(\d+)*\./i", $url, $match_id); if(isset($match_id[1]) && $match_id[1]){ $id = $match_id[1]; //获取价格 $price_url = "http://p.3.cn/prices/mgets?skuIds=J_$id,J_&type=1"; $price_content = file_get_contents($price_url); if(!empty($price_content)){ $price_content_arr = json_decode($price_content); echo mb_convert_encoding('商品价格为:', 'GBK', 'UTF-8') . $price_content_arr[0]->p . '<br>'; } $img_url = "http://www.jd.com/bigimage.aspx?id=$id"; $img_content = get_content_by_url($img_url, $ip); $title = preg_replace(array("/\(/", "/\)/", "/\./", "/\*/", "/\?/", "/\//"), array("\(", "\)", "\.", "\*", "\?", "\/"), $title); preg_match("/<img src=\"(.*?)\" alt=\"$title\"/i", $img_content, $match_img); if(isset($match_img[1]) && $match_img[1]){ $img = $match_img[1]; echo mb_convert_encoding('图片地址为:', 'GBK', 'UTF-8') . $img . '<br>'; echo "<img src='$img' width=300 height=300><br>"; }else{ echo mb_convert_encoding('没有获取到图片:', 'GBK', 'UTF-8') . '<br>'; } }else{ echo mb_convert_encoding('没有获取到商品id,程序终止:', 'GBK', 'UTF-8'); exit; } function get_rand_ip($ip_arr){ if(empty($ip_arr)){ return false; } $ip_count = count($ip_arr); $rand_num = rand(0, $ip_count-1); return trim($ip_arr[$rand_num]); } function get_ips(){ $fp = fopen('ip.txt', 'r+'); $ip_arr = array(); while($line=fgets($fp)){ array_push($ip_arr, $line); } fclose($fp); return $ip_arr; } function get_content_by_url($url, $ip = '127.0.0.1'){ if(empty($url)){ return; } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 5.0)'); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); if(!empty($ip)){ curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:' . $ip, 'CLIENT-IP:' . $ip)); //构造IP } $content = curl_exec($ch); return $content; }
抱歉,没找到上传附件的地方,ip.txt没上传。您能够百度下,有不少ip库,下载一个就能用,一行一个ipphp