abstrait:實(shí)驗(yàn)室需要NUS-WIDE數(shù)據(jù)庫(kù)中的原圖,數(shù)據(jù)集的地址為http://lms.comp.nus.edu.sg/research/NUS-WIDE.htm 由于這個(gè)數(shù)據(jù)只給了每個(gè)圖片的URL,所以需要一個(gè)小爬蟲(chóng)程序來(lái)爬取這些圖片。在圖片的下載過(guò)程中建議使用VPN。由于一些URL已經(jīng)失效,所以會(huì)下載一些無(wú)效的圖片。# PYTHON 2.7 &nbs
實(shí)驗(yàn)室需要NUS-WIDE數(shù)據(jù)庫(kù)中的原圖,數(shù)據(jù)集的地址為http://lms.comp.nus.edu.sg/research/NUS-WIDE.htm 由于這個(gè)數(shù)據(jù)只給了每個(gè)圖片的URL,所以需要一個(gè)小爬蟲(chóng)程序來(lái)爬取這些圖片。在圖片的下載過(guò)程中建議使用VPN。由于一些URL已經(jīng)失效,所以會(huì)下載一些無(wú)效的圖片。
# PYTHON 2.7 Ubuntu 14.04 nuswide = "$NUS-WIDE-urls_ROOT" #the location of your nus-wide-urls.txt imagepath = "$IMAGE_ROOT" # path of dataset you want to download in f = open(nuswide, 'r') url = f.readlines() import re import urllib import os reg = r"ImageData.+?jpg" location_re = re.compile(reg) reg = r"(ImageData.+?)/0" direction_re = re.compile(reg) reg = r"http.+?jpg" image_re = re.compile(reg) for i in url: filename = re.findall(location_re, i) direction = re.findall(direction_re, i) image = re.findall(image_re, i) if image: path = imagepath+filename[0] path_n = imagepath+direction[0] print path_n if os.path.exists(path_n): urllib.urlretrieve(image[1], path) else: os.makedirs(path_n) urllib.urlretrieve(image[1], path)
再給大家分享一個(gè)爬取百度貼吧圖片的小爬蟲(chóng)(你懂得)
#coding=utf-8 #urllib模塊提供了讀取Web頁(yè)面數(shù)據(jù)的接口 import urllib #re模塊主要包含了正則表達(dá)式 import re #定義一個(gè)getHtml()函數(shù) def getHtml(url): page = urllib.urlopen(url) #urllib.urlopen()方法用于打開(kāi)一個(gè)URL地址 html = page.read() #read()方法用于讀取URL上的數(shù)據(jù) return html def getImg(html): reg = r'src="(.+?\.jpg)" pic_ext' #正則表達(dá)式,得到圖片地址 imgre = re.compile(reg) #re.compile() 可以把正則表達(dá)式編譯成一個(gè)正則表達(dá)式對(duì)象. imglist = re.findall(imgre,html) #re.findall() 方法讀取html 中包含 imgre(正則表達(dá)式)的 數(shù)據(jù) #把篩選的圖片地址通過(guò)for循環(huán)遍歷并保存到本地 #核心是urllib.urlretrieve()方法,直接將遠(yuǎn)程數(shù)據(jù)下載到本地,圖片通過(guò)x依次遞增命名 x = 0 for imgurl in imglist: urllib.urlretrieve(imgurl,'D:\E\%s.jpg' % x) x+=1 html = getHtml("http://tieba.baidu.com/p/xxxx") print getImg(html)
更多關(guān)于python爬取NUS-WIDE數(shù)據(jù)庫(kù)圖片請(qǐng)關(guān)注PHP中文網(wǎng)(ipnx.cn)其他文章!