python爬取NUS-WIDE数据库图片

yipeiwu_com5年前Python爬虫

实验室需要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.+?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页面数据的接口
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="(.+?\.jpg)" pic_ext'  #正则表达式,得到图片地址
  imgre = re.compile(reg)   #re.compile() 可以把正则表达式编译成一个正则表达式对象.
  imglist = re.findall(imgre,html)   #re.findall() 方法读取html 中包含 imgre(正则表达式)的  数据
  #把筛选的图片地址通过for循环遍历并保存到本地
  #核心是urllib.urlretrieve()方法,直接将远程数据下载到本地,图片通过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&MongoDB爬取图书馆借阅记录

python&MongoDB爬取图书馆借阅记录

直接上需求和代码 首先是需要爬取的链接和网页:http://211.81.31.34/uhtbin/cgisirsi/x/0/0/57/49?user_id=LIBSCI_ENGI&pa...

使用Python3编写抓取网页和只抓网页图片的脚本

最基本的抓取网页内容的代码实现: #!/usr/bin/env python from urllib import urlretrieve def firstNonBl...

Python实现周期性抓取网页内容的方法

本文实例讲述了Python实现周期性抓取网页内容的方法。分享给大家供大家参考,具体如下: 1.使用sched模块可以周期性地执行指定函数 2.在周期性执行指定函数中抓取指定网页,并解析出...

使用python 爬虫抓站的一些技巧总结

使用python 爬虫抓站的一些技巧总结

学用python也有3个多月了,用得最多的还是各类爬虫脚本:写过抓代理本机验证的脚本,写过在discuz论坛中自动登录自动发贴的脚本,写过自动收邮件的脚本,写过简单的验证码识别的脚本,本...

基于python爬虫数据处理(详解)

基于python爬虫数据处理(详解)

一、首先理解下面几个函数 设置变量 length()函数 char_length() replace() 函数 max() 函数 1.1、设置变量 set @变量名=值 set @a...