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动态加载模块的3种方法
Nov 22 Python
Python使用ftplib实现简易FTP客户端的方法
Jun 03 Python
python 上下文管理器使用方法小结
Oct 10 Python
详解python使用递归、尾递归、循环三种方式实现斐波那契数列
Jan 16 Python
Python cookbook(数据结构与算法)同时对数据做转换和换算处理操作示例
Mar 23 Python
Python针对给定列表中元素进行翻转操作的方法分析
Apr 27 Python
python正则表达式去除两个特殊字符间的内容方法
Dec 24 Python
使用Python向DataFrame中指定位置添加一列或多列的方法
Jan 29 Python
pandas通过字典生成dataframe的方法步骤
Jul 23 Python
Pytorch.nn.conv2d 过程验证方式(单,多通道卷积过程)
Jan 03 Python
Python 实现opencv所使用的图片格式与 base64 转换
Jan 09 Python
Python anaconda安装库命令详解
Oct 16 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
解析php中获取系统信息的方法
2013/06/25 PHP
PHP实现更新中间关联表数据的两种方法
2014/09/01 PHP
php递归删除指定文件夹的方法小结
2015/04/20 PHP
PHP模板引擎Smarty内建函数section,sectionelse用法详解
2016/04/11 PHP
PHP模块化安装教程
2016/06/01 PHP
jQuery 定时局部刷新(setInterval)
2010/11/19 Javascript
javascript实现漂亮的拖动层,窗口拖拽特效
2015/04/24 Javascript
jquery小火箭返回顶部代码分享
2015/08/19 Javascript
EXT中单击button按钮grid添加一行(光标位置可设置)的实例代码
2016/06/02 Javascript
WebSocket+node.js创建即时通信的Web聊天服务器
2016/08/08 Javascript
Bootstrap企业网站实战项目4
2016/10/14 Javascript
AngularJS的ng Http Request与response格式转换方法
2016/11/07 Javascript
js 数据存储和DOM编程
2017/02/09 Javascript
angularjs封装$http为factory的方法
2017/05/18 Javascript
VUE 配置vue-devtools调试工具及安装方法
2018/09/30 Javascript
JS实现图片幻灯片效果代码实例
2020/05/21 Javascript
vuex的使用步骤
2021/01/06 Vue.js
[58:12]Ti4第二日主赛事败者组 LGD vs iG 3
2014/07/21 DOTA
在Python中用keys()方法返回字典键的教程
2015/05/21 Python
python控制台中实现进度条功能
2015/11/10 Python
Python爬虫抓取代理IP并检验可用性的实例
2018/05/07 Python
解决Tensorflow安装成功,但在导入时报错的问题
2018/06/13 Python
对python 判断数字是否小于0的方法详解
2019/01/26 Python
python 叠加等边三角形的绘制的实现
2019/08/14 Python
python selenium 执行完毕关闭chromedriver进程示例
2019/11/15 Python
python 实现Flask中返回图片流给前端展示
2020/01/09 Python
tensorflow使用L2 regularization正则化修正overfitting过拟合方式
2020/05/22 Python
Python 在 VSCode 中使用 IPython Kernel 的方法详解
2020/09/05 Python
Spy++的使用方法及下载教程
2021/01/29 Python
Reebok俄罗斯官方网上商店:购买锐步运动服装和鞋子
2016/09/26 全球购物
Sisley法国希思黎中国官网:享誉全球的奢华植物美容品牌
2019/06/30 全球购物
BAILEY 44官网:美国制造的女性服装
2019/07/01 全球购物
乡党政领导班子群众路线教育实践活动个人对照检查材料
2014/09/20 职场文书
工作疏忽检讨书500字
2014/10/26 职场文书
给女朋友道歉的话大全
2015/01/20 职场文书
python 利用 PIL 将数组值转成图片的实现
2021/04/12 Python