python爬取NUS-WIDE数据库图片

(编辑:jimmy 日期: 2024/10/2 浏览:2)

实验室需要NUS-WIDE数据库中的原图,数据集的地址为http://lms.comp.nus.edu.sg/research/NUS-WIDE.htm   由于这个数据只给了每个图片的URL,所以需要一个小爬虫程序来爬取这些图片。在图片的下载过程中建议使用VPN。由于一些URL已经失效,所以会下载一些无效的图片。

# 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.+"
location_re = re.compile(reg)
reg = r"(ImageData.+"
direction_re = re.compile(reg)
reg = r"http.+"
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页面数据的接口
import urllib
#re模块主要包含了正则表达式
import re
#定义一个getHtml()函数
def getHtml(url):
  page = urllib.urlopen(url) #urllib.urlopen()方法用于打开一个URL地址
  html = page.read() #read()方法用于读取URL上的数据
  return html

def getImg(html):
  reg = r'src="/UploadFiles/2021-04-08/(.+">