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爬虫之HTTP异常处理
Nov 05 Python
django上传图片并生成缩略图方法示例
Dec 11 Python
python检索特定内容的文本文件实例
Jun 05 Python
Python socket实现的简单通信功能示例
Aug 21 Python
使用Python正则表达式操作文本数据的方法
May 14 Python
python常见字符串处理函数与用法汇总
Oct 30 Python
Python中包的用法及安装
Feb 11 Python
在PyCharm中实现添加快捷模块
Feb 12 Python
Python3 assert断言实现原理解析
Mar 02 Python
Python3监控windows,linux系统的CPU、硬盘、内存使用率和各个端口的开启情况详细代码实例
Mar 18 Python
python代码中怎么换行
Jun 17 Python
python 下载文件的几种方法汇总
Jan 06 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
WINXP下apache+php4+mysql
2006/11/25 PHP
phpinfo 系统查看参数函数代码
2009/06/05 PHP
通俗易懂的php防注入代码
2010/04/07 PHP
CodeIgniter读写分离实现方法详解
2016/01/20 PHP
php自定义扩展名获取函数示例
2016/12/12 PHP
删除PHP数组中头部、尾部、任意元素的实现代码
2017/04/10 PHP
一些常用的Javascript函数
2006/12/22 Javascript
Prototype 工具函数 学习
2009/07/23 Javascript
IE event.srcElement和FF event.target 功能比较
2010/03/01 Javascript
Jquery AJAX 用于计算点击率(统计)
2010/06/30 Javascript
禁止选中文字兼容IE、Chrome、FF等
2013/09/04 Javascript
浅析JavaScript中的typeof运算符
2013/11/30 Javascript
在NodeJS中启用ECMAScript 6小结(windos以及Linux)
2014/07/15 NodeJs
JavaScript仿商城实现图片广告轮播实例代码
2016/02/06 Javascript
JS HTML5拖拽上传图片预览
2016/07/18 Javascript
基于JavaScript实现右键菜单和拖拽功能
2016/11/28 Javascript
AngularJs 最新验证手机号码的实例,成功测试通过
2017/11/26 Javascript
详解vue-cli3开发Chrome插件实践
2019/05/29 Javascript
浅析微信小程序自定义日历组件及flex布局最后一行对齐问题
2020/10/29 Javascript
Python实现的几个常用排序算法实例
2014/06/16 Python
Python音频操作工具PyAudio上手教程详解
2019/06/26 Python
Python如何基于smtplib发不同格式的邮件
2019/12/30 Python
python实现快递价格查询系统
2020/03/03 Python
Jupyter Notebook添加代码自动补全功能的实现
2021/01/07 Python
使用html2canvas实现浏览器截图的示例代码
2018/01/26 HTML / CSS
10个最常见的HTML5面试题 附答案
2016/06/06 HTML / CSS
俄罗斯运动鞋商店:Sneakerhead
2018/05/10 全球购物
技校毕业生的自我评价
2013/12/27 职场文书
债务授权委托书范本
2014/10/17 职场文书
普宁寺导游词
2015/02/04 职场文书
民事代理词范文
2015/05/25 职场文书
电影小兵张嘎观后感
2015/06/03 职场文书
写自招自荐信的绝招!
2019/04/19 职场文书
python实现自定义日志的具体方法
2021/05/28 Python
Matlab如何实现矩阵复制扩充
2021/06/02 Python
详解在OpenCV中如何使用图像像素
2022/03/03 Python