用python爬取并下载花瓣网美女图片

核心代码如下:

import urllib2
import json
import threading
 
class myThread(threading.Thread):
    def __init__(self, imgurl, filename):
        threading.Thread.__init__(self)
        self.imgurl = imgurl
        self.filename = filename
 
    def run(self):
        print 'downloading: ' + self.imgurl
        downfile(self.imgurl, self.filename)
 
def downfile(imgurl, filename):
    img_req = urllib2.Request(imgurl)
    opener = urllib2.build_opener()
    img_resp = opener.open(img_req)
    try:
        out = open(filename, 'wb')
        out.write(img_resp.read())
        out.flush()
        out.close()
    except:
        print 'error'
 
if __name__ == "__main__":
 
    #fname = 'http://img.hb.aicdn.com/c9e6dbfd0a685049734a7c4ff85846797f2cc10b66279-mMb1JX'
    #downfile(fname, 'aa.jpg')
     
    surl = 'http://huaban.com/pins/799598182/?j2j0w21y'
    surl = 'http://huaban.com/pins/587945163/?j2j3nm8w'
 
    hb = urllib2.Request(surl)
    hb.add_header('X-Requested-With', 'XMLHttpRequest')
    hb.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3047.4 Safari/537.36')
    html = urllib2.urlopen(hb).read()
    #print html
    obj = json.loads(html)
    #type(obj)
    #print obj\['pin'\]\['board'\]\['pins'\]
    imgs = obj\['pin'\]\['board'\]\['pins'\]
    preurl = 'http://img.hb.aicdn.com/'
    for img in imgs:
        imgurl = preurl + img\['file'\]\['key'\]
        #print imgurl
        myThread(imgurl, img\['file'\]\['key'\] + '.jpg').start()

转载:<https://www.tongpankt.com/4606>;
白俊遥博客

王如棋博客
请先登录后发表评论
  • 最新评论
  • 总共0条评论