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 相关文章推荐
mysql 之通过配置文件链接数据库
Aug 12 Python
python实现自动解数独小程序
Jan 21 Python
基于Python实现用户管理系统
Feb 26 Python
一文秒懂python读写csv xml json文件各种骚操作
Jul 04 Python
Python 获取windows桌面路径的5种方法小结
Jul 15 Python
解决Python正则表达式匹配反斜杠''\''问题
Jul 17 Python
详解python播放音频的三种方法
Sep 23 Python
python基于plotly实现画饼状图代码实例
Dec 16 Python
python实现飞船大战
Apr 24 Python
Python Selenium XPath根据文本内容查找元素的方法
Dec 07 Python
Python使用tkinter制作在线翻译软件
Feb 22 Python
Python实现简单的俄罗斯方块游戏
Sep 25 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中将时间差转换为字符串提示的实现代码
2011/08/08 PHP
30个最好的jQuery 灯箱插件分享
2011/04/25 Javascript
解析JavaScript中点号“.”的多义性
2013/12/02 Javascript
javascript匿名函数应用示例介绍
2014/03/07 Javascript
基于JavaScript创建动态Dom
2015/12/08 Javascript
js实现的奥运倒计时时钟效果代码
2015/12/09 Javascript
使用Script元素发送JSONP请求的方法
2016/06/12 Javascript
AngularJS入门教程之多视图切换用法示例
2016/11/02 Javascript
js控制按钮,防止频繁点击响应的实例
2017/02/15 Javascript
jquery中$.fn和图片滚动效果实现的必备知识总结
2017/04/21 jQuery
BootStrap selectpicker后台动态绑定数据
2017/06/01 Javascript
基于JSON数据格式详解
2017/08/31 Javascript
jQuery实现可兼容IE6的遮罩功能详解
2017/09/19 jQuery
Vue.js 点击按钮显示/隐藏内容的实例代码
2018/02/08 Javascript
layui问题之模拟select点击事件的实例讲解
2018/08/15 Javascript
如何解决webpack-dev-server代理常切换问题
2019/01/09 Javascript
layui select 禁止点击的实现方法
2019/09/05 Javascript
Python3.x和Python2.x的区别介绍
2013/02/12 Python
python的dict,set,list,tuple应用详解
2014/07/24 Python
Python实现从URL地址提取文件名的方法
2015/05/15 Python
numpy自动生成数组详解
2017/12/15 Python
Python解析并读取PDF文件内容的方法
2018/05/08 Python
python实现写数字文件名的递增保存文件方法
2018/10/25 Python
python聚类算法解决方案(rest接口/mpp数据库/json数据/下载图片及数据)
2019/08/28 Python
python代码如何注释
2020/06/01 Python
python语言的优势是什么
2020/06/17 Python
eBay法国购物网站:eBay.fr
2017/10/21 全球购物
介绍一下代理模式(Proxy)
2014/10/17 面试题
银行简历自我评价
2014/02/11 职场文书
教师党的群众路线教育实践活动学习心得体会
2014/10/30 职场文书
2014年度工作总结报告
2014/12/15 职场文书
2015年检验员工作总结范文
2015/04/30 职场文书
入党申请书怎么写?
2019/06/21 职场文书
导游词之山东八大关
2019/12/18 职场文书
python解决12306登录验证码的实现
2021/04/18 Python
MySQL 数据表操作
2022/05/04 MySQL