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之入门(五)缩进和选择
May 27 Python
Python httplib模块使用实例
Apr 11 Python
Python使用defaultdict读取文件各列的方法
May 11 Python
Python实现求解括号匹配问题的方法
Apr 17 Python
python中pika模块问题的深入探究
Oct 13 Python
python 实现读取一个excel多个sheet表并合并的方法
Feb 12 Python
python实现海螺图片的方法示例
May 12 Python
Python with用法:自动关闭文件进程
Jul 10 Python
Python之指数与E记法的区别详解
Nov 21 Python
解决jupyter notebook import error但是命令提示符import正常的问题
Apr 15 Python
python中pandas库中DataFrame对行和列的操作使用方法示例
Jun 14 Python
python能自学吗
Jun 18 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字符串截取中文截取2,单字节截取模式
2007/12/10 PHP
php+ajax做仿百度搜索下拉自动提示框(有实例)
2012/08/21 PHP
kindeditor 加入七牛云上传的实例讲解
2017/11/12 PHP
splice slice区别
2006/10/09 Javascript
jQuery插件 tabBox实现代码
2010/02/09 Javascript
jQuery中andSelf()方法用法实例
2015/01/08 Javascript
jQuery仿gmail实现fixed布局的方法
2015/05/27 Javascript
JS去除空格和换行的正则表达式(推荐)
2016/06/14 Javascript
微信小程序自定义组件
2017/08/16 Javascript
JS实现点击链接切换显示隐藏内容的方法
2017/10/19 Javascript
js中getter和setter用法实例分析
2018/08/14 Javascript
详解将微信小程序接口Promise化并使用async函数
2019/08/05 Javascript
js实现跳一跳小游戏
2020/07/31 Javascript
H5 js点击按钮复制文本到粘贴板
2020/11/19 Javascript
[01:32]TI珍贵瞬间系列(一)
2020/08/26 DOTA
Python中的Matplotlib模块入门教程
2015/04/15 Python
浅谈python 四种数值类型(int,long,float,complex)
2016/06/08 Python
Python中利用aiohttp制作异步爬虫及简单应用
2018/11/29 Python
python中利用matplotlib读取灰度图的例子
2019/12/07 Python
对Python中 \r, \n, \r\n的彻底理解
2020/03/06 Python
使用tensorflow框架在Colab上跑通猫狗识别代码
2020/04/26 Python
python代码区分大小写吗
2020/06/17 Python
python飞机大战游戏实例讲解
2020/12/04 Python
CSS3 实现的火焰动画
2020/12/07 HTML / CSS
利用三角函数在canvas上画虚线的方法
2018/01/11 HTML / CSS
详解如何使用rem或viewport进行移动端适配
2020/08/14 HTML / CSS
俄罗斯天然和有机产品、健康生活网上商店:Fitomarket.ru
2020/10/09 全球购物
最新大学生自我评价
2013/09/24 职场文书
车工岗位职责
2013/11/26 职场文书
初婚未育证明
2014/01/15 职场文书
实习单位证明范例
2014/11/17 职场文书
故宫导游词
2015/01/31 职场文书
简爱读书笔记
2015/06/26 职场文书
2016班级元旦联欢会开幕词
2016/03/04 职场文书
2019初中学生入团申请书
2019/06/27 职场文书
【海涛教你打DOTA】死灵飞龙第一视角解说
2022/04/01 DOTA