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 SQLAlchemy基本操作和常用技巧(包含大量实例,非常好)
May 06 Python
Python通过正则表达式选取callback的方法
Jul 18 Python
Python实现的下载网页源码功能示例
Jun 13 Python
python决策树之C4.5算法详解
Dec 20 Python
解决Python 爬虫URL中存在中文或特殊符号无法请求的问题
May 11 Python
对django中render()与render_to_response()的区别详解
Oct 16 Python
python爬虫之urllib,伪装,超时设置,异常处理的方法
Dec 19 Python
Python中的异常处理try/except/finally/raise用法分析
Feb 28 Python
使用浏览器访问python写的服务器程序
Oct 10 Python
python文件操作的简单方法总结
Nov 07 Python
Pytorch在dataloader类中设置shuffle的随机数种子方式
Jan 14 Python
pip/anaconda修改镜像源,加快python模块安装速度的操作
Mar 04 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
我的论坛源代码(一)
2006/10/09 PHP
echo, print, printf 和 sprintf 区别
2006/12/06 PHP
php+redis消息队列实现抢购功能
2018/02/08 PHP
jquery 插件之仿“卓越亚马逊”首页弹出菜单效果
2008/12/25 Javascript
Javascript学习笔记5 类和对象
2010/01/11 Javascript
CutePsWheel javascript libary 控制输入文本框为可使用滚轮控制的js库
2010/02/07 Javascript
javascript伸缩型菜单实现代码
2015/11/16 Javascript
阻止表单提交按钮多次提交的完美解决方法
2016/05/16 Javascript
javascript作用域、作用域链(菜鸟必看)
2016/06/16 Javascript
JavaScript生成验证码并实现验证功能
2016/09/24 Javascript
用原生JS实现简单的多选框功能
2017/06/12 Javascript
node.js中grunt和gulp的区别详解
2017/07/17 Javascript
JS实现带导航城市列表以及输入搜索功能
2018/01/04 Javascript
[02:08]2014DOTA2国际邀请赛 430专访:力争取得小组前二
2014/07/11 DOTA
python使用内存zipfile对象在内存中打包文件示例
2014/04/30 Python
浅谈Python生成器generator之next和send的运行流程(详解)
2017/05/08 Python
python制作图片缩略图
2019/04/30 Python
Python PyQt5模块实现窗口GUI界面代码实例
2020/05/12 Python
Python中的全局变量如何理解
2020/06/04 Python
Cpython解释器中的GIL全局解释器锁
2020/11/09 Python
canvas 阴影和图形变换的示例代码
2018/01/02 HTML / CSS
正宗的日本零食和糖果订阅盒:Bokksu
2019/11/21 全球购物
Python如何定义一个函数
2015/09/01 面试题
毕业生优秀推荐信
2013/11/26 职场文书
教师实习自我鉴定
2013/12/11 职场文书
法律进企业活动方案
2014/03/04 职场文书
爱国主义教育演讲稿
2014/08/26 职场文书
党的群众路线教育实践活动自我剖析材料
2014/10/08 职场文书
教师批评与自我批评总结
2014/10/16 职场文书
巾帼标兵事迹材料
2014/12/26 职场文书
爱牙日宣传活动总结
2015/02/05 职场文书
2015大学生自我评价范文
2015/03/03 职场文书
行政人事专员岗位职责
2015/04/07 职场文书
商标侵权律师函
2015/05/27 职场文书
公司食堂管理制度
2015/08/05 职场文书
css height属性中的calc方法详解
2021/06/03 HTML / CSS