python批量下载兰科植物网站的图片,并重命名文件
生活随笔
收集整理的這篇文章主要介紹了
python批量下载兰科植物网站的图片,并重命名文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
該程序為下載 http://www.orchidspecies.com/ 蘭花網站圖片,并以名字命名圖片的小爬蟲。
requests,chardet 第三方模塊需要自己下載。
# -*- coding: utf-8 -*- import re,os,requests,urllib2,chardet,time,sys stdi,stdo,stde=sys.stdin,sys.stdout,sys.stderr reload(sys) sys.stdin,sys.stdout,sys.stderr=stdi,stdo,stde sys.setdefaultencoding('utf-8') #獲取網頁源代碼(提取所需內容) def get_content(url,reg):headers = {'User-agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.48'}request = requests.get(url,timeout=20,headers = headers)content = request.textwant=reg.findall(content)return want#獲取網頁源代碼(用于轉碼)-為了解決http://www.orchidspecies.com/indexcattleyo.htm亂碼 def for_change(url,reg):headers={'User-agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.48'}request=urllib2.Request(url,headers=headers)req=urllib2.urlopen(request,timeout=20)res=req.read()enc=chardet.detect(res)['encoding']print u'該網頁使用'+enc+u'編碼'content=res.decode(enc).encode('utf-8')want=reg.findall(content)return want#創建文件夾 def create_folder(path):if not os.path.exists(path):os.mkdir(path)#保存圖片 def download_image(imageurl,imagename):data=requests.get(imageurl,timeout=20).contentwith open(imagename,'wb') as f:f.write(data)#寫入記事本備份 def create_txt(txtname,data):with open(txtname,'a') as f:f.write(data)#下載每個種 def load_picture(everyurl,url,path,n):p3=Truex=1a3=re.compile(r'src="(.+?\.\w{3})"',re.I)#獲取每個種的網址和名字if everyurl.find('">')!=-1:picurl=everyurl.split('">')[0]name=' '.join(everyurl.split('">')[1].strip().split())name=name.replace(' x ',u' × ').replace('<P>','').replace("?","").replace("!","")if name.find(u' × ')!=-1:name=name.split()[0]+' '+name.split()[1]+' '+name.split()[2]else:name=name.split()[0]+' '+name.split()[1]#創建種的文件夾if not os.path.exists(path+name):os.mkdir(path+name)print name#獲取圖片網址并下載while p3: try:u4=get_content(url+'/'+picurl,a3)p3=Falsefor u5 in u4:p4=Trueif u5 not in('orphotdir/scent.jpg','orphotdir/deepshade.jpg','orphotdir/partialshade.jpg','orphotdir/partialsun.jpg','orphotdir/sun.jpg','orphotdir/tempcold.jpg','orphotdir/tempcool.jpg','orphotdir/tempint.jpg','orphotdir/temphot.jpg','orphotdir/spring.jpg','orphotdir/summer.jpg','orphotdir/fall.jpg','orphotdir/winter.jpg'):while p4:try:imageurl=url+'/'+u5imagename=path+name+"\\%s %s-%s.jpg" % (name,str(n),str(x))download_image(imageurl,imagename)print str(n)+'-'+str(x)x+=1p4=Falseexcept:print str(n)+'-'+str(x)+' is not download,please wait 10 second!'time.sleep(10)p3=Falseexcept:txtname=u'出錯.txt'data=url+'/'+picurl+' '+name+' '+time.strftime('%Y-%m-%d %X', time.localtime())+'\n'with open(txtname,'a') as f:f.write(data)print u'第'+str(n)+u'個種網頁獲取失敗,請稍候10秒'time.sleep(10)if __name__ == '__main__':path='D:\\orchid_only\\'create_folder(path)n=0 #計數alll=[] #存放所有種網址#提取一級網址url="http://www.orchidspecies.com"a1=re.compile(r'SIZE=2><A href="(index\w.+?)">',re.I)p1=Trueprint urlwhile p1:try:u1=content1=get_content(url,a1)u1=list(set(u1))print u'獲取一級網址成功,開始提取二級網址'p1=Falseexcept:print u'獲取一級網址失敗,10秒后重新連接'time.sleep(10)#提取二級網址a2=re.compile(r'<P><LI><a href="(.+?)</A>',re.I)for u2 in u1:u2=url+'/'+u2p2=Trueprint u2while p2:try:u3=get_content(u2,a2)print len(u3)if len(u3)==0:u3=for_change(u2,a2)print len(u3)alll.extend(u3)print u'獲取二級網址成功,存放成功'p2=Falseexcept:print u'獲取二級網址失敗,10秒后重新連接'time.sleep(10)#提取每個種的圖片 for everyurl in alll:n+=1print u'正在下載第'+str(n)+u'個種'load_picture(everyurl,url,path,n) print 'over,共下載蘭花'+str(n)+'種'總結
以上是生活随笔為你收集整理的python批量下载兰科植物网站的图片,并重命名文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 建立syslink双核工程和make运行
- 下一篇: wsl 2 中安装docker