Last active
August 29, 2015 14:14
-
-
Save lizheming/c98716a7466bcaffffd0 to your computer and use it in GitHub Desktop.
百度地图书店搜索结果抓取
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getPages( total ) { | |
var url = "http://ditu.baidu.com/?newmap=1&reqflag=pcmap&biz=1&pcevaname=pc2&da_par=direct&qt=s&from=webmap&da_src=pcmappg.poi.page&wd=%E4%B9%A6%E5%BA%97&c=131&sug=0&ie=utf-8&wd2=&l=12&src=7&pn=1&db=0&on_gel=1&gr=3&addr=0&nn="; | |
var start = 0, urls = []; | |
do { | |
urls.push( url + start ); | |
start += 10; | |
} while( start < total ); | |
return urls; | |
} | |
function getScript(url) { | |
return new Promise(function(resolve, reject) { | |
var xhr = new XMLHttpRequest; | |
xhr.open("GET", url, true); | |
xhr.responseType = "json"; | |
xhr.onload = function() { | |
xhr.status !== 200 ? reject( Error( xhr.statusText ) ) : resolve( xhr.response ); | |
} | |
xhr.onerror = function() { | |
reject( Error( xhr.statusText ) ); | |
} | |
xhr.send(null); | |
}) | |
} | |
var addrs = []; | |
getPages( 760 ).map( getScript ).reduce(function(seq, json) { | |
return seq.then(function(){return json}).then(function(pos) { | |
console.log( pos ); | |
addrs = addrs.concat( pos.content ); | |
return addrs; | |
}) | |
}, Promise.resolve() ).then(function() { | |
console.log( addrs ); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment