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的pycurl包用法简介
Nov 13 Python
Python程序中用csv模块来操作csv文件的基本使用教程
Mar 03 Python
基于hashlib模块--加密(详解)
Jun 21 Python
Python反射的用法实例分析
Feb 11 Python
Python调用adb命令实现对多台设备同时进行reboot的方法
Oct 15 Python
实时获取Python的print输出流方法
Jan 07 Python
python如何获取当前文件夹下所有文件名详解
Jan 25 Python
PyQt5 对图片进行缩放的实例
Jun 18 Python
keras分类之二分类实例(Cat and dog)
Jul 09 Python
Python类绑定方法及非绑定方法实例解析
Oct 09 Python
Python控制鼠标键盘代码实例
Dec 08 Python
python实现图像随机裁剪的示例代码
Dec 10 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里的JS打印函数
2006/10/09 PHP
不用数据库的多用户文件自由上传投票系统(3)
2006/10/09 PHP
实用函数2
2007/11/08 PHP
关于svn冲突的解决方法
2013/06/21 PHP
PHP环境搭建(php+Apache+mysql)
2016/11/14 PHP
php解析base64数据生成图片的方法
2016/12/06 PHP
php分页查询的简单实现代码
2017/03/14 PHP
通过length属性判断jquery对象是否存在
2013/10/18 Javascript
javascript判断office版本示例
2014/04/11 Javascript
Jquery性能优化详解
2014/05/15 Javascript
JavaScript实现的简单烟花特效代码
2015/10/20 Javascript
js创建jsonArray传输至后台及后台全面解析
2016/04/11 Javascript
jQuery监听文件上传实现进度条效果的方法
2016/10/16 Javascript
React Native开发封装Toast与加载Loading组件示例
2018/09/08 Javascript
LayUi使用switch开关,动态的去控制它是否被启用的方法
2019/09/21 Javascript
微信小程序绑定手机号获取验证码功能
2019/10/22 Javascript
[40:16]TFT vs Mski Supermajor小组赛C组 BO3 第二场 6.3
2018/06/04 DOTA
Python爬取读者并制作成PDF
2015/03/10 Python
python实现kMeans算法
2017/12/21 Python
Python元组及文件核心对象类型详解
2018/02/11 Python
tensorflow更改变量的值实例
2018/07/30 Python
python 矢量数据转栅格数据代码实例
2019/09/30 Python
Python箱型图绘制与特征值获取过程解析
2019/10/22 Python
python urllib和urllib3知识点总结
2021/02/08 Python
美国经典刺绣和字母儿童服装特卖:Smocked Auctions
2018/07/16 全球购物
大四本科生的自我评价
2013/12/30 职场文书
校园十佳歌手策划书
2014/01/22 职场文书
商务助理求职信范文
2014/04/20 职场文书
创先争优活动心得体会
2014/09/04 职场文书
爱心捐款感谢信
2015/01/20 职场文书
社区党建工作总结2015
2015/05/13 职场文书
会议承办单位欢迎词
2019/07/09 职场文书
JavaScript 语句之常用 for 循环详解
2021/03/29 Javascript
代码解析React中setState同步和异步问题
2021/06/03 Javascript
Spring Boot两种全局配置和两种注解的操作方法
2021/06/29 Java/Android
「我的青春恋爱物语果然有问题。-妄言录-」第20卷封面公开
2022/03/21 日漫