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之集合的关系
Sep 24 Python
python实现从web抓取文档的方法
Sep 26 Python
Python 基础教程之闭包的使用方法
Sep 29 Python
Python反射用法实例简析
Dec 22 Python
从django的中间件直接返回请求的方法
May 30 Python
Flask框架Flask-Principal基本用法实例分析
Jul 23 Python
python3.6 如何将list存入txt后再读出list的方法
Jul 02 Python
利用ImageAI库只需几行python代码实现目标检测
Aug 09 Python
Python爬取新型冠状病毒“谣言”新闻进行数据分析
Feb 16 Python
Django Admin 上传文件到七牛云的示例代码
Jun 20 Python
jupyter notebook更换皮肤主题的实现
Jan 07 Python
python实现ROA算子边缘检测算法
Apr 05 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 register_globals 值为on与off的理解
2013/09/26 PHP
php读取mysql的简单实例
2014/01/15 PHP
php多个文件及图片上传实例详解
2014/11/10 PHP
windows server 2008/2012安装php iis7 mysql环境搭建教程
2016/06/30 PHP
CentOS7.0下安装PHP5.6.30服务的教程详解
2018/09/29 PHP
PHP+mysql实现的三级联动菜单功能示例
2019/02/15 PHP
动态控制Table的js代码
2007/03/07 Javascript
Javascript & DHTML 实例编程(教程)(三)初级实例篇1—上传文件控件实例
2007/06/02 Javascript
Extjs4 消息框去掉关闭按钮(类似Ext.Msg.alert)
2013/04/02 Javascript
jQuery隐藏和显示效果实现
2016/04/06 Javascript
js图片轮播手动切换特效
2017/01/12 Javascript
knockoutjs模板实现树形结构列表
2017/07/31 Javascript
一步步教你利用Canvas对图片进行处理
2017/09/19 Javascript
解决Vue使用swiper动态加载数据,动态轮播数据显示白屏的问题
2018/09/27 Javascript
js微信分享接口调用详解
2019/07/23 Javascript
vue-cli点击实现全屏功能
2020/03/07 Javascript
[01:21:58]守擂赛DOTA2第一周决赛
2020/04/22 DOTA
[01:08:24]DOTA2-DPC中国联赛 正赛 RNG vs Phoenix BO3 第一场 2月5日
2021/03/11 DOTA
Python代码的打包与发布详解
2014/07/30 Python
Python实现抓取城市的PM2.5浓度和排名
2015/03/19 Python
详解在Python程序中解析并修改XML内容的方法
2015/11/16 Python
Swift 3.0在集合类数据结构上的一些新变化总结
2016/07/11 Python
Python简单格式化时间的方法【strftime函数】
2016/09/18 Python
浅谈Pycharm中的Python Console与Terminal
2019/01/17 Python
GitHub 热门:Python 算法大全,Star 超过 2 万
2019/04/29 Python
Python SQLAlchemy入门教程(基本用法)
2019/11/11 Python
如何Tkinter模块编写Python图形界面
2020/10/14 Python
怎样在程序里获得一个空指针
2015/01/24 面试题
英文求职信结束语大全
2013/10/26 职场文书
企业给企业的表扬信
2014/01/13 职场文书
球队口号
2014/06/18 职场文书
领导干部群众路线对照检查材料
2014/11/05 职场文书
初三学生语文考试作弊检讨书
2014/12/14 职场文书
捐助倡议书
2015/01/19 职场文书
幼儿园欢迎词范文
2015/01/26 职场文书
刑事上诉状范文
2015/05/22 职场文书