天天看點

php爬去商城一類資料,利用php封裝函數抓取京東商城省市縣資料

error_reporting(E_ALL);

require './source/class/class_core.php';

$discuz = & discuz_core::instance();

//$cachelist = array('plugin');

$discuz->cachelist = $cachelist;

$discuz->init();

function uc_fopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE, $ip = '', $timeout = 50, $block = false) {

$return = '';

$matches = parse_url($url);

!isset($matches['host']) && $matches['host'] = '';

!isset($matches['path']) && $matches['path'] = '';

!isset($matches['query']) && $matches['query'] = '';

!isset($matches['port']) && $matches['port'] = '';

$host = $matches['host'];

$path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/';

$port = !empty($matches['port']) ? $matches['port'] : 80;

if($post) {

$out = "POST $path HTTP/1.0\r\n";

$out .= "Accept: **\r\n";

//$out .= "Referer: $boardurl\r\n";

$out .= "Accept-Language: zh-cn\r\n";

$out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";

$out .= "Host: $host\r\n";

$out .= "Connection: Close\r\n";

$out .= "Cookie: $cookie\r\n\r\n";

}else {

$out = "GET $path HTTP/1.0\r\n";

$out .= "Accept: *

@$fp = fopen("sql.txt","w");

if(!$fp){

echo "system error";

exit();

}

for($i=0;$i<34;$i++){

//echo "insert into ultrax_common_district_Tmp (code,name,upcode,level) value (".$return[$i]['id'].",'".$return[$i]['name']."',0,0);

";

$city=uc_fopen('http://d.360buy.com/area/get?fid='.$return[$i]['id']);

sleep(1);

for($j=0;$j

//$fileData="insert into ultrax_common_district_Tmp (code,name,upcode,level) value (".$city[$j]['id'].",'".$city[$j]['name']."',".$return[$i]['id'].",1);\n";

//fwrite($fp,$fileData);

//echo "insert into ultrax_common_district_Tmp (code,name,upcode,level) value ('".$city[$j]['id']."',".$city[$j]['name']."',".$return[$i]['id'].",1);

";

$area=uc_fopen('http://d.360buy.com/area/get?fid='.$city[$j]['id']);

$length=count($area);

for($k=0;$k

$fileData="insert into ultrax_common_district_Tmp (code,name,upcode,level) value (".$area[$k]['id'].",'".$area[$k]['name']."',".$city[$j]['id'].",2);\n";

fwrite($fp,$fileData);

//echo "insert into ultrax_common_district_Tmp (code,name,upcode,level) value (".$area[$k]['id'].",'".$area[$k]['name']."',".$city[$j]['id'].",2);\n";

}

sleep(1);

}

}

fclose($fp);

exit;

echo  $script=<<

var data=$return;

var city=null;

function getdata(id){

$.ajax({

url: "http://d.360buy.com/area/get?fid="+id,

// the name of the callback parameter, as specified by the YQL service

jsonp: "callback",

// tell jQuery we're expecting JSONP

dataType: "jsonp",

// tell YQL what we want and that we want JSON

data: {

format: "json"

},

// work with the response

success: function(response) {

for(var j=0;j

console.log("insert into city (name,upid) values ('"+response[j]["name"]+"',"+id+");

");

}

}

});

}

for(var i= 0;i<34;i++){

var province=data[i];

//setTimeout(getdata(data[i]['id']),1000000000000*i);

for(var j=0;j

alert(province);

setTimeout(getdata(province[j]['id']),1000000000000*j);

}

//document.write("insert into province (name,upid) values ("+data[i]["name"]+"',0);

");

}