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 urlopen()函数 示例分享
Jun 12 Python
给Python初学者的一些编程技巧
Apr 03 Python
Python的Django框架中的URL配置与松耦合
Jul 15 Python
Python使用Matplotlib模块时坐标轴标题中文及各种特殊符号显示方法
May 04 Python
Django Sitemap 站点地图的实现方法
Apr 29 Python
django rest framework 实现用户登录认证详解
Jul 29 Python
使用批处理脚本自动生成并上传NuGet包(操作方法)
Nov 19 Python
python使用itchat模块给心爱的人每天发天气预报
Nov 25 Python
Python 3.8 新功能大揭秘【新手必学】
Feb 05 Python
如何使用python传入不确定个数参数
Feb 18 Python
浅谈python中的多态
Jun 15 Python
Sentry的安装、配置、使用教程(Sentry日志手机系统)
Jul 23 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
Codeigniter+PHPExcel实现导出数据到Excel文件
2014/06/12 PHP
四个PHP非常实用的功能
2015/09/29 PHP
PHP引用的调用方法分析
2016/04/25 PHP
支付宝服务窗API接口开发php版本
2016/07/20 PHP
php大小写转换函数(strtolower、strtoupper)用法介绍
2017/11/17 PHP
PHP匿名函数(闭包函数)详解
2019/03/22 PHP
php生成静态页面并实现预览功能
2019/06/27 PHP
PHP pthreads v3在centos7平台下的安装与配置操作方法
2020/02/21 PHP
js和as的稳定传值问题解决
2013/07/14 Javascript
jQuery怎么解析Json字符串(Json格式/Json对象)
2013/08/09 Javascript
javascript实现原生ajax的几种方法介绍
2013/09/21 Javascript
函数式 JavaScript(一)简介
2014/07/07 Javascript
jquery中页面Ajax方法$.load的功能使用介绍
2014/10/20 Javascript
angularjs学习笔记之三大模块(modal,controller,view)
2015/09/26 Javascript
基于jQuery的网页影音播放器jPlayer的基本使用教程
2016/03/08 Javascript
详解Vue2.X的路由管理记录之 钩子函数(切割流水线)
2017/05/02 Javascript
解决Vue 浏览器后退无法触发beforeRouteLeave的问题
2017/12/24 Javascript
vue实现与安卓、IOS交互的方法
2018/11/02 Javascript
js实现鼠标拖拽缩放div实例代码
2019/03/25 Javascript
js实现淘宝首页的banner栏效果
2019/11/26 Javascript
python和shell获取文本内容的方法
2018/06/05 Python
Tornado Web Server框架编写简易Python服务器
2018/07/28 Python
python读取word 中指定位置的表格及表格数据
2019/10/23 Python
python 实现快速生成连续、随机字母列表
2019/11/28 Python
python 常用日期处理-- datetime 模块的使用
2020/09/02 Python
Canvas 像素处理之改变透明度的实现代码
2019/01/08 HTML / CSS
联想西班牙官网:Lenovo西班牙
2018/08/28 全球购物
美国购买体育、音乐会和剧院门票网站:SelectATicket
2019/09/08 全球购物
德国消费电子产品购物网站:Guter Kauf
2020/09/15 全球购物
解释一下钝化(Swap out)
2016/12/26 面试题
保安拾金不昧表扬信
2014/01/15 职场文书
爱国主义演讲稿
2014/05/07 职场文书
火箭队口号
2014/06/18 职场文书
班子个人四风问题整改措施
2014/10/04 职场文书
党支部创先争优公开承诺书
2015/04/30 职场文书
党支部综合考察意见
2015/06/01 职场文书