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 itchat包爬取微信好友头像形成矩形头像集的方法

使用python itchat包爬取微信好友头像形成矩形头像集的方法

初学python,我们必须干点有意思的事!从微信下手吧! 头像集样例如下: 大家可以发朋友圈开启辨认大赛哈哈~ 话不多说,直接上代码,注释我写了比较多,大家应该能看懂 impor...

python定向爬取淘宝商品价格

python爬虫学习之定向爬取淘宝商品价格,供大家参考,具体内容如下 import requests import re def getHTMLText(url): try:...

Python爬虫实现网页信息抓取功能示例【URL与正则模块】

本文实例讲述了Python爬虫实现网页信息抓取功能。分享给大家供大家参考,具体如下: 首先实现关于网页解析、读取等操作我们要用到以下几个模块 import urllib import...

Python 利用scrapy爬虫通过短短50行代码下载整站短视频

Python 利用scrapy爬虫通过短短50行代码下载整站短视频

近日,有朋友向我求助一件小事儿,他在一个短视频app上看到一个好玩儿的段子,想下载下来,可死活找不到下载的方法。这忙我得帮,少不得就抓包分析了一下这个app,找到了视频的下载链接,帮他解...

python爬虫模拟浏览器的两种方法实例分析

python爬虫模拟浏览器的两种方法实例分析

本文实例讲述了python爬虫模拟浏览器的两种方法。分享给大家供大家参考,具体如下: 爬虫爬取网站出现403,因为站点做了防爬虫的设置 一、Herders 属性 爬取CSDN博客 i...