Python爬虫获取图片并下载保存至本地的实例


Posted in Python onJune 01, 2018

1、抓取煎蛋网上的图片。

2、代码如下:

import urllib.request
import os
#to open the url
def url_open(url):
 req=urllib.request.Request(url)
 req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.3; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0')
 response=urllib.request.urlopen(url)
 html=response.read()
 return html
#to get the num of page like 1,2,3,4...
def get_page(url):
 html=url_open(url).decode('utf-8')
 a=html.find('current-comment-page')+23 #add the 23 offset th arrive at the [2356]
 b=html.find(']',a)
 #print(html[a:b])
 return html[a:b]
#find the url of imgs and return the url of arr
def find_imgs(url):
 html=url_open(url).decode('utf-8')
 img_addrs=[]
 a=html.find('img src=')
 while a!=-1:
  b=html.find('.jpg',a,a+255) # if false : return -1
  if b!=-1:
   img_addrs.append('http:'+html[a+9:b+4])
  else:
   b=a+9
  a=html.find('img src=',b)
 #print(img_addrs)  
 return img_addrs
  #print('http:'+each)
  
#save the imgs 
def save_imgs(folder,img_addrs):
 for each in img_addrs:
  filename=each.split('/')[-1] #get the last member of arr,that is the name
  with open(filename,'wb') as f:
   img = url_open(each)
   f.write(img)
 
def download_mm(folder='mm',pages=10):
 os.mkdir(folder)
 os.chdir(folder)
 url='http://jandan.net/ooxx/'
 page_num=int(get_page(url))
 
 for i in range(pages):
  page_num -= i
  page_url = url + 'page-' + str(page_num) + '#comments'
  img_addrs=find_imgs(page_url)
  save_imgs(folder,img_addrs)
  
if __name__ == '__main__':
 download_mm()

以上这篇Python爬虫获取图片并下载保存至本地的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python安装与使用redis的方法
Apr 19 Python
Python环境下安装使用异步任务队列包Celery的基础教程
May 07 Python
python统计字母、空格、数字等字符个数的实例
Jun 29 Python
Python os.rename() 重命名目录和文件的示例
Oct 25 Python
对python中大文件的导入与导出方法详解
Dec 28 Python
python的pytest框架之命令行参数详解(下)
Jun 27 Python
Django框架 查询Extra功能实现解析
Sep 04 Python
python爬虫开发之selenium模块详细使用方法与实例全解
Mar 09 Python
Python使用Excel将数据写入多个sheet
May 16 Python
QML用PathView实现轮播图
Jun 03 Python
.img/.hdr格式转.nii格式的操作
Jul 01 Python
Python pandas对excel的操作实现示例
Jul 21 Python
python操作mysql代码总结
Jun 01 #Python
Python使用pylab库实现绘制直方图功能示例
Jun 01 #Python
python的格式化输出(format,%)实例详解
Jun 01 #Python
Python获取昨天、今天、明天开始、结束时间戳的方法
Jun 01 #Python
python面向对象多线程爬虫爬取搜狐页面的实例代码
May 31 #Python
Python中if elif else及缩进的使用简述
May 31 #Python
python基于物品协同过滤算法实现代码
May 31 #Python
You might like
PHP+AJAX实现无刷新注册(带用户名实时检测)
2007/01/02 PHP
PHP中对各种加密算法、Hash算法的速度测试对比代码
2014/07/08 PHP
php实现求相对时间函数
2015/06/15 PHP
php获取微信共享收货地址的方法
2017/12/21 PHP
PHP之认识(二)关于Traits的用法详解
2019/04/11 PHP
再谈Yii Framework框架中的事件event原理与应用
2020/04/07 PHP
侧栏跟随滚动的简单实现代码
2013/03/18 Javascript
js截取中英文字符串、标点符号无乱码示例解读
2014/04/17 Javascript
node.js读取文件到字符串的方法
2015/06/29 Javascript
jQuery实现的仿select功能代码
2015/08/19 Javascript
js正则表达式验证邮件地址
2015/11/12 Javascript
JS跨域解决方案之使用CORS实现跨域
2016/04/14 Javascript
仿百度换肤功能的简单实例代码
2016/07/11 Javascript
微信小程序 vidao实现视频播放和弹幕的功能
2016/11/02 Javascript
AngularJS自定义控件实例详解
2016/12/13 Javascript
js 用于检测类数组对象的函数方法
2017/05/02 Javascript
jQuery的时间datetime控件在AngularJs中的使用实例(分享)
2017/08/17 jQuery
详解vue中引入stylus及报错解决方法
2017/09/22 Javascript
微信小程序导航栏滑动定位功能示例(实现CSS3的positionsticky效果)
2019/01/24 Javascript
微信小程序搜索功能(附:小程序前端+PHP后端)
2019/02/28 Javascript
react 生命周期实例分析
2020/05/18 Javascript
JavaScript实现HSL拾色器
2020/05/21 Javascript
在vue中动态修改css其中一个属性值操作
2020/12/07 Vue.js
Python标准库与第三方库详解
2014/07/22 Python
Python中文件I/O高效操作处理的技巧分享
2017/02/04 Python
利用python批量修改word文件名的方法示例
2017/10/17 Python
python 3.7.0 下pillow安装方法
2018/08/27 Python
解决django 向mysql中写入中文字符出错的问题
2020/05/18 Python
为什么是 Python -m
2020/06/19 Python
python中封包建立过程实例
2021/02/18 Python
Ibatis如何调用存储过程
2015/05/15 面试题
收银员岗位职责
2014/02/07 职场文书
高中军训感想300字
2014/03/04 职场文书
公证委托书格式
2014/09/13 职场文书
中学生检讨书范文
2014/11/03 职场文书
创业计划书之游泳馆
2019/09/16 职场文书