Python 爬虫图片简单实现


Posted in Python onJune 01, 2017

Python 爬虫图片简单实现

经常在逛知乎,有时候希望把一些问题的图片集中保存起来。于是就有了这个程序。这是一个非常简单的图片爬虫程序,只能爬取已经刷出来的部分的图片。由于对这一部分内容不太熟悉,所以只是简单说几句然后记录代码,不做过多的讲解。感兴趣的可以直接拿去用。亲测对于知乎等网站是可用的。

上一篇分享了通过url打开图片的方法,目的就是先看看爬取到的图片时什么样,然后再筛选一下保存。

这里用到了requests库来获取页面信息,需要注意的是,获取页面信息的时候需要一个header,用以把程序伪装成浏览器去访问服务器,不然可能会被服务器拒绝。然后用BeautifulSoup来过滤多余信息得到图片地址。得到图片后,根据图片的大小过滤掉一些头像、表情包之类的小图片。最后打开或者保存图片的时候选择就比较多了,OpenCV,skimage,PIL等都可以。

程序如下:

# -*- coding=utf-8 -*-
import requests as req
from bs4 import BeautifulSoup
from PIL import Image
from io import BytesIO
import os
from skimage import io

url = "https://www.zhihu.com/question/37787176"
headers = {'User-Agent' : 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Mobile Safari/537.36'}
response = req.get(url,headers=headers)
content = str(response.content)
#print content

soup = BeautifulSoup(content,'lxml')
images = soup.find_all('img')
print u"共有%d张图片" % len(images)

if not os.path.exists("images"):
  os.mkdir("images")

for i in range(len(images)):
  img = images[i]
  print u"正在处理第%d张图片..." % (i+1)
  img_src = img.get('src')
  if img_src.startswith("http"):
    ## use PIL
    '''
    print img_src
    response = req.get(img_src,headers=headers)
    image = Image.open(BytesIO(response.content))
    w,h = image.size
    print w,h
    img_path = "images/" + str(i+1) + ".jpg"
    if w>=500 and h>500:
      #image.show()
      image.save(img_path)

    '''

    ## use OpenCV
    import numpy as np
    import urllib
    import cv2

    resp = urllib.urlopen(img_src)

    image = np.asarray(bytearray(resp.read()), dtype="uint8")
    image = cv2.imdecode(image, cv2.IMREAD_COLOR)
    w,h = image.shape[:2]
    print w,h
    img_path = "images/" + str(i+1) + ".jpg"
    if w>=400 and h>400:
      cv2.imshow("Image", image)
      cv2.waitKey(3000)
      ##cv2.imwrite(img_path,image)

    ## use skimage

    ## image = io.imread(img_src)
    ## w,h = image.shape[:2]
    ## print w,h
    #io.imshow(image)
    #io.show()

    ## img_path = "images/" + str(i+1) + ".jpg"
    ## if w>=500 and h>500:
      ## image.show()
      ## image.save(img_path)
      ## io.imsave(img_path,image)

print u"处理完成!"

这里给出了多种选择,供参考。

Python 相关文章推荐
Python运行报错UnicodeDecodeError的解决方法
Jun 07 Python
python时间日期函数与利用pandas进行时间序列处理详解
Mar 13 Python
Pandas Shift函数的基础入门学习笔记
Nov 16 Python
pygame游戏之旅 添加游戏界面按键图形
Nov 20 Python
python fuzzywuzzy模块模糊字符串匹配详细用法
Aug 29 Python
简单了解为什么python函数后有多个括号
Dec 19 Python
python路径的写法及目录的获取方式
Dec 26 Python
在Python IDLE 下调用anaconda中的库教程
Mar 09 Python
Python基于pip实现离线打包过程详解
May 15 Python
python用Tkinter做自己的中文代码编辑器
Sep 07 Python
python安装mysql的依赖包mysql-python操作
Jan 01 Python
Python基础之Socket通信原理
Apr 22 Python
Python 通过URL打开图片实例详解
Jun 01 #Python
git使用.gitignore设置不生效或不起作用问题的解决方法
Jun 01 #Python
python 内置函数filter
Jun 01 #Python
python读取二进制mnist实例详解
May 31 #Python
Python算术运算符实例详解
May 31 #Python
Python简单的制作图片验证码实例
May 31 #Python
详解python的webrtc库实现语音端点检测
May 31 #Python
You might like
如何设置mysql允许外网访问
2013/06/04 PHP
PHP的变量类型和作用域详解
2014/03/12 PHP
php获取YouTube视频信息的方法
2015/02/11 PHP
PHP的文件操作与算法实现的面试题示例
2015/08/10 PHP
php 策略模式原理与应用深入理解
2019/09/25 PHP
动态改变textbox的宽高的js
2006/10/26 Javascript
JavaScript与Div对层定位和移动获得坐标的实现代码
2010/09/08 Javascript
javascript垃圾收集机制与内存泄漏详细解析
2013/11/11 Javascript
js验证输入是否为手机号码或电话号码示例
2013/12/30 Javascript
Javascript动画的实现原理浅析
2015/03/02 Javascript
js获取url传值的方法
2015/12/18 Javascript
利用JS实现数字增长
2016/07/28 Javascript
JavaScript正则获取地址栏中参数的方法
2017/03/02 Javascript
详解nodejs爬虫程序解决gbk等中文编码问题
2017/04/06 NodeJs
微信小程序教程系列之设置标题栏和导航栏(7)
2020/06/29 Javascript
AngularJS+Bootstrap3多级导航菜单的实现代码
2017/08/16 Javascript
vue 指令之气泡提示效果的实现代码
2018/10/18 Javascript
JS中实现浅拷贝和深拷贝的代码详解
2019/06/05 Javascript
JS实现拖动模糊框特效
2020/08/25 Javascript
[08:40]Navi Vs Newbee
2018/06/07 DOTA
Python获取DLL和EXE文件版本号的方法
2015/03/10 Python
python轻松实现代码编码格式转换
2015/03/26 Python
用Python实现一个简单的线程池
2015/04/07 Python
浅谈Python基础之I/O模型
2017/05/11 Python
程序员写Python时的5个坏习惯,你有几条?
2018/11/26 Python
关于python之字典的嵌套,递归调用方法
2019/01/21 Python
PIL图像处理模块paste方法简单使用详解
2019/07/17 Python
Python学习笔记之函数的定义和作用域实例详解
2019/08/13 Python
python实现画出e指数函数的图像
2019/11/21 Python
Selenium使用Chrome模拟手机浏览器方法解析
2020/04/10 Python
新西兰廉价汽车租赁:Snap Rentals
2018/09/14 全球购物
意外伤害赔偿协议书
2014/09/16 职场文书
4S店销售内勤岗位职责
2015/04/13 职场文书
2015七夕情人节宣传语
2015/07/14 职场文书
汽车销售员工作总结
2015/08/12 职场文书
担保书格式范文
2015/09/22 职场文书