python爬取NUS-WIDE數(shù)據(jù)庫圖片
更新時間:2016年10月05日 10:45:19 作者:leo-sun
本文給大家分享的是使用Python制作爬蟲爬取圖片的小程序,非常的簡單,但是很實用,有需要的小伙伴可以參考下
實驗室需要NUS-WIDE數(shù)據(jù)庫中的原圖,數(shù)據(jù)集的地址為http://lms.comp.nus.edu.sg/research/NUS-WIDE.htm 由于這個數(shù)據(jù)只給了每個圖片的URL,所以需要一個小爬蟲程序來爬取這些圖片。在圖片的下載過程中建議使用VPN。由于一些URL已經(jīng)失效,所以會下載一些無效的圖片。
# 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)
再給大家分享一個爬取百度貼吧圖片的小爬蟲(你懂得)
#coding=utf-8 #urllib模塊提供了讀取Web頁面數(shù)據(jù)的接口 import urllib #re模塊主要包含了正則表達式 import re #定義一個getHtml()函數(shù) def getHtml(url): page = urllib.urlopen(url) #urllib.urlopen()方法用于打開一個URL地址 html = page.read() #read()方法用于讀取URL上的數(shù)據(jù) return html def getImg(html): reg = r'src="(.+?\.jpg)" pic_ext' #正則表達式,得到圖片地址 imgre = re.compile(reg) #re.compile() 可以把正則表達式編譯成一個正則表達式對象. imglist = re.findall(imgre,html) #re.findall() 方法讀取html 中包含 imgre(正則表達式)的 數(shù)據(jù) #把篩選的圖片地址通過for循環(huán)遍歷并保存到本地 #核心是urllib.urlretrieve()方法,直接將遠程數(shù)據(jù)下載到本地,圖片通過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)
您可能感興趣的文章:
- Python實現(xiàn)批量讀取圖片并存入mongodb數(shù)據(jù)庫的方法示例
- python3+PyQt5使用數(shù)據(jù)庫表視圖
- python3+PyQt5使用數(shù)據(jù)庫窗口視圖
- python聚類算法解決方案(rest接口/mpp數(shù)據(jù)庫/json數(shù)據(jù)/下載圖片及數(shù)據(jù))
- python 讀取數(shù)據(jù)庫并繪圖的實例
- Python操作MySQL數(shù)據(jù)庫9個實用實例
- python Django連接MySQL數(shù)據(jù)庫做增刪改查
- python連接oracle數(shù)據(jù)庫實例
- Python讀寫Redis數(shù)據(jù)庫操作示例
- python連接mongodb操作數(shù)據(jù)示例(mongodb數(shù)據(jù)庫配置類)
- Python的Flask框架與數(shù)據(jù)庫連接的教程
- Python使用py2neo操作圖數(shù)據(jù)庫neo4j的方法詳解
相關文章
詳解Python執(zhí)行py文件是否需要可執(zhí)行權限
這篇文章主要通過幾個案例為大家詳細介紹一下在Python中執(zhí)行py文件是否需要可執(zhí)行權限,文中的示例代碼講解詳細,對我們學習Python有一定幫助,需要的可以了解一下2023-03-03python把數(shù)組中的數(shù)字每行打印3個并保存在文檔中的方法
今天小編就為大家分享一篇python把數(shù)組中的數(shù)字每行打印3個并保存在文檔中的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07Windows10下Tensorflow2.0 安裝及環(huán)境配置教程(圖文)
這篇文章主要介紹了Windows10下Tensorflow2.0 安裝及環(huán)境配置教程(圖文),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11