python爬蟲——爬取NUS-WIDE數據庫圖片


   實驗室需要NUS-WIDE數據庫中的原圖,數據集的地址為http://lms.comp.nus.edu.sg/research/NUS-WIDE.htm   由於這個數據只給了每個圖片的URL,所以需要一個小爬蟲程序來爬取這些圖片。在圖片的下載過程中建議使用VPN。由於一些URL已經失效,所以會下載一些無效的圖片。

 1 # PYTHON 2.7     Ubuntu 14.04
 2 nuswide = "$NUS-WIDE-urls_ROOT" #the location of your nus-wide-urls.txt
 3 imagepath = "$IMAGE_ROOT" # path of dataset you want to download in
 4 f = open(nuswide, 'r')
 5 url = f.readlines()
 6 import re
 7 import urllib
 8 import os
 9 reg = r"ImageData.+?jpg"
10 location_re = re.compile(reg)
11 reg = r"(ImageData.+?)/0"
12 direction_re = re.compile(reg)
13 reg = r"http.+?jpg"
14 image_re = re.compile(reg)
15 for i in url:
16     filename = re.findall(location_re, i)
17     direction = re.findall(direction_re, i)
18     image = re.findall(image_re, i)
19     if image:
20         path = imagepath+filename[0]
21         path_n = imagepath+direction[0]
22         print path_n
23         if os.path.exists(path_n):
24             urllib.urlretrieve(image[1], path)
25         else:
26             os.makedirs(path_n)
27             urllib.urlretrieve(image[1], path)

 update 1:

我在使用數據集的nus-wide-urls.txt文件時,為了避免‘\’的轉義字符問題,將其中的'\'替換為了'/'。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM