- from multiprocessing import Pool
- import urllib2
- import json
- import os, sys
- photos = {}
- api_file = "api.json"
- api = "http://api.lovebizhi.com/macos_v4.php?a=category&spdy=1&tid=3&order=hot&color_id=3&device=105&uuid=436e4ddc389027ba3aef863a27f6e6f9&mode=0&retina=0&client_id=1008&device_id=31547324&model_id=105&size_id=0&channel_id=70001&screen_width=1920&screen_height=1200&bizhi_width=1920&bizhi_height=1200&version_code=19&language=zh-Hans&jailbreak=0&mac=&p={pid}"
- def file_get_contents(url):
- try:
- body = urllib2.urlopen(url).read()
- return body
- except urllib2.HTTPError, e:
- print 'HttpError occurred. error url %s, error code %s' % (url, e.code)
- except KeyboardInterrupt:
- print 'canceled'
- except:
- print 'Uknown HttpError occurred.'
- return False
- def download_api(pid):
- global api
- page = api.replace("{pid}", str(pid))
- print "fetch api %d" % pid
- return file_get_contents(page)
- def download_img(pid):
- url = "http://s.qdcdn.com/c/%s,1920,1200.jpg" % pid
- path = "imgs/%s.jpg" % pid
- img = file_get_contents(url);
- print "fetch img %d url %s" % (pid, url)
- if False != img:
- f = open(path, "w")
- f.write(img)
- f.close()
- if False == os.path.isfile(api_file) :
- pages = range(1, 6)
- pool = Pool(5)
- results = pool.map(download_api, pages)
- pool.close()
- pool.join()
- for html in results:
- f = open(api_file, "a")
- f.write(str(html) + "\\n")
- f.close()
- jsons = open(api_file, "r").readlines()
- for x in jsons:
- y = x.replace('\\r\\n', "")
- j = json.loads(y)
- for z in j["data"]:
- if False == photos.has_key(z["file_id"]):
- photos[z["file_id"]] = z["file_id"];
- try:
- pool = Pool(10)
- pool.map(download_img, photos)
- pool.close()
- pool.join()
- except KeyboardInterrupt:
- print "Ctrl-c pressed ..."
- sys.exit(1)
- #该片段来自于http://www.codesnippet.cn/detail/1509201410398.html
来源: http://www.codesnippet.cn/detail/1509201410398.html