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使用Beautiful Soup包编写爬虫时的一些关键点
Jan 20 Python
Python实现批量检测HTTP服务的状态
Oct 27 Python
Python中Django发送带图片和附件的邮件
Mar 31 Python
Python实现判断字符串中包含某个字符的判断函数示例
Jan 08 Python
Python使用Selenium+BeautifulSoup爬取淘宝搜索页
Feb 24 Python
Python全面分析系统的时域特性和频率域特性
Feb 26 Python
python GUI库图形界面开发之PyQt5菜单栏控件QMenuBar的详细使用方法与实例
Feb 28 Python
python实现遍历文件夹图片并重命名
Mar 23 Python
基于python实现破解滑动验证码过程解析
May 28 Python
Python OpenCV读取中文路径图像的方法
Jul 02 Python
python中的对数log函数表示及用法
Dec 09 Python
Python 匹配文本并在其上一行追加文本
May 11 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注入实例
2006/10/09 PHP
一个没有MYSQL数据库支持的简易留言本的编写
2006/10/09 PHP
Smarty中调用FCKeditor的方法
2014/10/27 PHP
php递归函数怎么用才有效
2018/02/24 PHP
Thinkphp 在api开发中异常返回依然是html的解决方式
2019/10/16 PHP
你必须知道的Javascript知识点之"深入理解作用域链"的介绍
2013/04/23 Javascript
动态改变div的z-index属性的简单实例
2013/08/08 Javascript
Javascript中的getUTCDay()方法使用详解
2015/06/10 Javascript
jquery实现点击查看更多内容控制段落文字展开折叠效果
2015/08/06 Javascript
Bootstrap模态对话框的简单使用
2016/04/29 Javascript
前端js文件合并的三种方式推荐
2016/05/19 Javascript
AngularJS实现树形结构(ztree)菜单示例代码
2016/09/18 Javascript
JS当前页面登录注册框,固定DIV,底层阴影的实例代码
2016/09/29 Javascript
Vue2.0 实现移动端图片上传功能
2018/05/30 Javascript
vue-router之nuxt动态路由设置的两种方法小结
2018/09/26 Javascript
elementUI中Table表格问题的解决方法
2018/12/04 Javascript
微信小程序 简易计算器实现代码实例
2019/09/02 Javascript
Python获取Linux系统下的本机IP地址代码分享
2014/11/07 Python
python实现数独算法实例
2015/06/09 Python
使用python和pygame绘制繁花曲线的方法
2018/02/24 Python
Python八大常见排序算法定义、实现及时间消耗效率分析
2018/04/27 Python
基于python3实现socket文件传输和校验
2018/07/28 Python
centos6.8安装python3.7无法import _ssl的解决方法
2018/09/17 Python
python 实现百度网盘非会员上传超过500个文件的方法
2021/01/07 Python
应届毕业生通用的自荐书范文
2014/02/07 职场文书
关于安全的标语
2014/06/10 职场文书
大学英语专业求职信
2014/06/21 职场文书
政工例会汇报材料
2014/08/26 职场文书
岳庙导游词
2015/02/04 职场文书
500字小学生检讨书
2015/02/19 职场文书
幼儿教师师德师风自我评价
2015/03/05 职场文书
2015年个人思想总结
2015/03/09 职场文书
实习单位意见
2015/06/04 职场文书
付款证明格式范文
2015/06/19 职场文书
结婚典礼主持词
2015/06/29 职场文书
详细聊聊浏览器是如何看闭包的
2021/11/11 Javascript