128进程,图片爬虫,增量更新,可作为标准组件

128进程,图片爬虫,增量更新,可作为标准组件,第1张

概述128进程,图片爬虫增量更新,可作为标准组件

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

#!/usr/bin/env pythonimport osimport os.pathimport urllibimport socketimport imghdrfrom multiprocessing import Poolfrom urlparse import urlparseresults = []exceptions = []def callback(result):    print 'result:',result    if result:        results.append(result)def path_exists(path):    fname = path.split('/')[-1]    for exist_fname in files:        if exist_fname.startswith(fname):            return exist_fname    return Falsedef retrIEve(url,path):    try:        print 'retrIEve:',url,' to',path        if os.path.exists(path):            return 'file exists:',path        elif path_exists(path):            return 'similar file:',path        urllib.urlretrIEve(url,path)        ftype = imghdr.what(path)        if ftype and ftype != path.split('.')[-1] and path.split('.')[-1] != 'jpg':            os.rename(path,path+'.'+ftype)        elif ftype is None:            os.rename(path,path+'.none')        return 'success:',path,ftype    except Exception as e:        exception = 'exception: ' + url + ' ' + path + ' | ' + str(e)        exceptions.append(exception)        return exceptionfiles = os.Listdir('./imgs')def main():    pool = Pool(processes=128)    exist_file = 0    socket.setdefaulttimeout(3)    with open('samples.log') as f:        for index,line in enumerate(f):            try:                count,url = line.split()            except:                print 'exception:',count,url                continue            # print 'main:',url            fname = urlparse(url).path.split('/')[-1]            path = './imgs/'+str(index)+'.'+count+'.'+fname            result = pool.apply_async(                    retrIEve,args=(url,path),callback=callback            )        print 'apply async done'    pool.close()    pool.join()    for e in exceptions:        print eif __name__ == '__main__':    main()

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的128进程,图片爬虫,增量更新,可作为标准组件全部内容,希望文章能够帮你解决128进程,图片爬虫,增量更新,可作为标准组件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1198921.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存