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 相关文章推荐
Pyhthon中使用compileall模块编译源文件为pyc文件
Apr 28 Python
Django中更新多个对象数据与删除对象的方法
Jul 17 Python
Ruby元编程基础学习笔记整理
Jul 02 Python
python如何在列表、字典中筛选数据
Mar 19 Python
jupyter notebook引用from pyecharts.charts import Bar运行报错
Apr 23 Python
Windows 64位下python3安装nltk模块
Sep 19 Python
python查看模块安装位置的方法
Oct 16 Python
matplotlib实现区域颜色填充
Mar 18 Python
Django中如何防范CSRF跨站点请求伪造攻击的实现
Apr 28 Python
使用 Python 清理收藏夹里已失效的网站
Dec 03 Python
python中count函数简单的实例讲解
Feb 06 Python
详解python with 上下文管理器
Sep 02 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中static静态变量的使用方法详解
2010/06/04 PHP
php 带逗号千位符数字的处理方法
2012/01/10 PHP
深入理解PHP之数组(遍历顺序)  Laruence原创
2012/06/13 PHP
使用PHP导出Redis数据到另一个Redis中的代码
2014/03/12 PHP
神盾加密解密教程(一)PHP变量可用字符
2014/05/28 PHP
php导出CSV抽象类实例
2014/09/24 PHP
php获取Google机器人访问足迹的方法
2015/04/15 PHP
laravel 解决Validator使用中出现的问题
2019/10/25 PHP
JavaScript支持的最大递归调用次数分析
2014/06/24 Javascript
实例讲解避免javascript冲突的方法
2016/01/03 Javascript
Web前端开发工具——bower依赖包管理工具
2016/03/29 Javascript
使用jsonp实现跨域获取数据实例讲解
2016/12/25 Javascript
Vue-Access-Control 前端用户权限控制解决方案
2017/12/01 Javascript
在vue中,v-for的索引index在html中的使用方法
2018/03/06 Javascript
vue自定义一个v-model的实现代码
2018/06/21 Javascript
Angular 实现输入框中显示文章标签的实例代码
2018/11/07 Javascript
python list语法学习(带例子)
2013/11/01 Python
python使用BeautifulSoup分页网页中超链接的方法
2015/04/04 Python
Python实现随机创建电话号码的方法示例
2018/12/07 Python
python 实现得到当前时间偏移day天后的日期方法
2018/12/31 Python
Gauss-Seidel迭代算法的Python实现详解
2019/06/29 Python
python如何从文件读取数据及解析
2019/09/19 Python
Python调用.net动态库实现过程解析
2020/06/05 Python
浅谈keras中的后端backend及其相关函数(K.prod,K.cast)
2020/06/29 Python
python中upper是做什么用的
2020/07/20 Python
PyQt实现计数器的方法示例
2021/01/18 Python
python中os.remove()用法及注意事项
2021/01/31 Python
百丽国际旗下购物网站:优购
2017/02/28 全球购物
橄榄树药房:OLIVEDA
2019/09/01 全球购物
听课评语大全
2014/04/30 职场文书
中央空调节能方案
2014/06/15 职场文书
欢迎标语大全
2014/06/21 职场文书
世界遗产导游词
2015/02/13 职场文书
团委副书记工作总结
2015/08/14 职场文书
OpenCV实现反阈值二值化
2021/11/17 Java/Android
SpringCloud项目如何解决log4j2漏洞
2022/04/10 Java/Android