python爬取NUS-WIDE数据库图片

yipeiwu_com4年前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爬虫获取百度首页内容教学

python爬虫获取百度首页内容教学

由传智播客教程整理,我们这里使用的是python2.7.x版本,就是2.7之后的版本,因为python3的改动略大,我们这里不用它。现在我们尝试一下url和网络爬虫配合的关系,爬浏览器首...

python正则表达式爬取猫眼电影top100

用正则表达式爬取猫眼电影top100,具体内容如下 #!/usr/bin/python # -*- coding: utf-8 -*- import json # 快速导...

Python爬取当当、京东、亚马逊图书信息代码实例

Python爬取当当、京东、亚马逊图书信息代码实例

注:1.本程序采用MSSQLserver数据库存储,请运行程序前手动修改程序开头处的数据库链接信息 2.需要bs4、requests、pymssql库支持 3.支持多线程 from...

python正则爬取某段子网站前20页段子(request库)过程解析

python正则爬取某段子网站前20页段子(request库)过程解析

首先还是谷歌浏览器抓包对该网站数据进行分析,结果如下: 该网站地址:http://www.budejie.com/text 该网站数据都是通过html页面进行展示,网站url默认为第...

Python3爬虫学习之应对网站反爬虫机制的方法分析

Python3爬虫学习之应对网站反爬虫机制的方法分析

本文实例讲述了Python3爬虫学习之应对网站反爬虫机制的方法。分享给大家供大家参考,具体如下: 如何应对网站的反爬虫机制 在访问某些网站的时候,网站通常会用判断访问是否带有头文件来鉴别...