<?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