Python批量抓取图片

Python批量抓取图片,第1张

概述Python批量抓取图片

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

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

# -*- Coding:utf-8 -*-# Coding=UTF-8import os,urllib,urllib2,reurl = u"http://image.baIDu.com/search/index?tn=baIDuimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=index&fr=&sf=1&fmq=&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&wIDth=&height=&face=0&istype=2&IE=utf-8&word=python&oq=python&rsp=-1"outpath = "t:\"def getHTML(url):    webfile = urllib.urlopen(url)    outHTML = webfile.read()    print outHTML    return outHTMLdef getimageList(HTML):    restr=ur'('    restr+=ur'http:\/\/[^\s,"]*\.jpg'    restr+=ur'|http:\/\/[^\s,"]*\.jpeg'    restr+=ur'|http:\/\/[^\s,"]*\.png'    restr+=ur'|http:\/\/[^\s,"]*\.gif'    restr+=ur'|http:\/\/[^\s,"]*\.bmp'    restr+=ur'|https:\/\/[^\s,"]*\.jpeg'        restr+=ur'|https:\/\/[^\s,"]*\.jpeg'    restr+=ur'|https:\/\/[^\s,"]*\.png'    restr+=ur'|https:\/\/[^\s,"]*\.gif'    restr+=ur'|https:\/\/[^\s,"]*\.bmp'    restr+=ur')'    HTMLurl = re.compile(restr)    imgList = re.findall(HTMLurl,HTML)    print imgList    return imgListdef download(imgList,page):    x = 1    for imgurl in imgList:        filepathname=str(outpath+'pic_%09d_%010d'%(page,x)+str(os.path.splitext(urllib2.unquote(imgurl).decode('utf8').split('/')[-1])[1])).lower()        print '[DeBUG] Download file :'+ imgurl+' >> '+filepathname        urllib.urlretrIEve(imgurl,filepathname)        x+=1def downImageNum(pagenum):    page = 1    pageNumber = pagenum    while(page <= pageNumber):        HTML = getHTML(url)#获得url指向的HTML内容        imageList = getimageList(HTML)#获得所有图片的地址,返回列表        download(imageList,page)#下载所有的图片        page = page+1if __name__ == '__main__':    downImageNum(1)

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

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

总结

以上是内存溢出为你收集整理的Python批量抓取图片全部内容,希望文章能够帮你解决Python批量抓取图片所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存