python 爬网站上的图片

最近在做bot的动物识别,最后根据大会给出来的测试数据,发现对简笔画的动物识别处于懵圈状态,识别效果很差~故我需要自己爬取一些简笔画的图片~
手写学习了某一网站的图片爬取:
这里写图片描述
附上代码:

# -*- coding: utf-8 -*-
import urllib
import re
import time
import os

#显示下载进度
def schedule(a,b,c):
  '''''
  a:已经下载的数据块
  b:数据块的大小
  c:远程文件的大小
   '''
  per = 100.0 * a * b / c
  if per > 100 :
    per = 100
  print '%.2f%%' % per

def getHtml(url):
  page = urllib.urlopen(url)
  html = page.read()
  return html

def downloadImg(html):
  reg = r'src="(.+?\.jpg)" pic_ext'
  imgre = re.compile(reg)
  imglist = re.findall(imgre, html)
  #定义文件夹的名字
  t = time.localtime(time.time())
  foldername = str(t.__getattribute__("tm_year"))+"-"+str(t.__getattribute__("tm_mon"))+"-"+str(t.__getattribute__("tm_mday"))
  picpath = 'H:\\getpic\\pic\\%s' % (foldername) #下载到的本地目录

  if not os.path.exists(picpath):   #路径不存在时创建一个
    os.makedirs(picpath)   
  x = 0
  for imgurl in imglist:
    target = picpath+'\\%s.jpg' % x
    print 'Downloading image to location: ' + target + '\nurl=' + imgurl
    image = urllib.urlretrieve(imgurl, target, schedule)
    x += 1
  return image;



if __name__ == '__main__':
  print '''         *************************************
      **      Welcome to use Spider   **
      **     Created on  2016-09-08   **
      **       @author:dcx         **
      *************************************'''

  html = getHtml("http://tieba.baidu.com/p/2460150866")

  downloadImg(html)
  print "Download has finished."

但是,我其实想爬取百度图片,这些图片来源于不同的网站,这没法直接用上面的代码,需要获取到每个图片的url 查找了各方资料,还是没搞明白 最后索性直接用ctr+s 保存网页就可以将网页上的图片保存下来~

其实我还是想学习怎么将百度图片上的数据爬下来~~继续努力 未完待续!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值