<?php
set_time_limit(0);
date_default_timezone_set("Asia/shanghai");
//自定义抓取图片地址
$url = 'http://www.amazon.cn/Apple-iPhone-5S-3G%E6%99%BA%E8%83%BD%E6%89%8B%E6%9C%BA/dp/B00FFVIPN8/ref=sr_1_1?ie=UTF8&qid=1395041969&sr=8-1&keywords=iphone5s';
$ip_arr = get_ips();
$ip = trim(get_rand_ip($ip_arr)); //随机ip
$content = get_content_by_url($url, $ip);
//获取标题
preg_match("/<span id=\"btAsinTitle\">[\s]*<span style=\"padding-left: 0\">[\s]*(.*?)[\s]*<\/span>/i", $content, $match_title);
if(isset($match_title[1]) && $match_title[1]){
$title = $match_title[1];
echo '标题为:' . $title . '<br />';
}else{
echo '没有获取到标题,程序终止:';
exit;
}
//获取价格
preg_match("/<b class=\"priceLarge\">¥[\s]*(.*?)<\/b>/i", $content, $match_price);
if(isset($match_title[1]) && $match_price[1]){
$price = $match_price[1];
echo '价格为:' . $price . '<br />';
}else{
echo '没有获取到价格,程序终止:';
}
//获取图片
preg_match("/<div class=\"main-image-inner-wrapper\">[\s]*<img src=\"(.*)?\"/i", $content, $match_img);
if(isset($match_img[1]) && $match_img[1]){
$img_url = $match_img[1];
echo '图片地址为:' . $img_url . '<br />';
echo "<img src='$img_url' width=300, height=300>";
}else{
echo '没有获取图片地址,程序终止:';
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库,下载一个就能用,一行一个ip