python实现下载指定网址所有图片的方法


Posted in Python onAugust 08, 2015

本文实例讲述了python实现下载指定网址所有图片的方法。分享给大家供大家参考。具体实现方法如下:

#coding=utf-8
#download pictures of the url
#useage: python downpicture.py www.baidu.com
import os
import sys
from html.parser import HTMLParser
from urllib.request import urlopen
from urllib.parse import urlparse
def getpicname(path):
  '''  retrive filename of url    '''
  if os.path.splitext(path)[1] == '':
    return None
  pr=urlparse(path)
  path='http://'+pr[1]+pr[2]
  return os.path.split(path)[1]
def saveimgto(path, urls):
  '''
  save img of url to local path
  '''
  if not os.path.isdir(path):
    print('path is invalid')
    sys.exit()
  else:
    for url in urls:
      of=open(os.path.join(path, getpicname(url)), 'w+b')
      q=urlopen(url)
      of.write(q.read())
      q.close()
      of.close()
class myhtmlparser(HTMLParser):
  '''put all src of img into urls'''
  def __init__(self):
    HTMLParser.__init__(self)
    self.urls=list()
    self.num=0
  def handle_starttag(self, tag, attr):
    if tag.lower() == 'img':
      srcs=[u[1] for u in attr if u[0].lower() == 'src']
      self.urls.extend(srcs)
      self.num = self.num+1
if __name__ == '__main__':
  url=sys.argv[1]
  if not url.startswith('http://'):
    url='http://' + sys.argv[1]
  parseresult=urlparse(url)
  domain='http://' + parseresult[1]
  q=urlopen(url)
  content=q.read().decode('utf-8', 'ignore')
  q.close()
  myparser=myhtmlparser()
  myparser.feed(content)
  for u in myparser.urls:
    if (u.startswith('//')):
      myparser.urls[myparser.urls.index(u)]= 'http:'+u
    elif u.startswith('/'):
      myparser.urls[myparser.urls.index(u)]= domain+u
  saveimgto(r'D:\python\song', myparser.urls)
  print('num of download pictures is {}'.format(myparser.num))

运行结果如下:

num of download pictures is 19

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
详解Python的Django框架中inclusion_tag的使用
Jul 21 Python
浅谈Python Opencv中gamma变换的使用详解
Apr 02 Python
python在每个字符后添加空格的实例
May 07 Python
Python RabbitMQ消息队列实现rpc
May 30 Python
python 列表,数组和矩阵sum的用法及区别介绍
Jun 28 Python
Python实现通过解析域名获取ip地址的方法分析
May 17 Python
Python3 JSON编码解码方法详解
Sep 06 Python
Eclipse配置python默认头过程图解
Apr 26 Python
浅谈keras中的Merge层(实现层的相加、相减、相乘实例)
May 23 Python
Python生成并下载文件后端代码实例
Aug 31 Python
Python中使用subprocess库创建附加进程
May 11 Python
Python matplotlib 利用随机函数生成变化图形
Apr 26 Python
Python实现多线程抓取妹子图
Aug 08 #Python
通过Python来使用七牛云存储的方法详解
Aug 07 #Python
Python爬虫框架Scrapy实战之批量抓取招聘信息
Aug 07 #Python
深入理解Python中命名空间的查找规则LEGB
Aug 06 #Python
举例详解Python中yield生成器的用法
Aug 05 #Python
Python中return语句用法实例分析
Aug 04 #Python
python函数形参用法实例分析
Aug 04 #Python
You might like
PHP 常见郁闷问题答解
2006/11/25 PHP
php中使用session_set_save_handler()函数把session保存到MySQL数据库实例
2014/11/06 PHP
PHP判断数据库中的记录是否存在的方法
2014/11/14 PHP
php如何获取文件的扩展名
2015/10/28 PHP
基于win2003虚拟机中apache服务器的访问
2017/08/01 PHP
一些易混淆且不常用的属性,希望有用
2007/01/29 Javascript
基于jquery的loading效果实现代码
2010/11/05 Javascript
js RuntimeObject() 获取ie里面自定义函数或者属性的集合
2010/11/23 Javascript
js页面滚动时层智能浮动定位实现(jQuery/MooTools)
2011/08/23 Javascript
JS代码放在head和body中的区别分析
2011/12/01 Javascript
详解AngularJS中自定义指令的使用
2015/06/17 Javascript
js实现的简洁网页滑动tab菜单效果代码
2015/08/24 Javascript
javascript模拟C#格式化字符串
2015/08/26 Javascript
js中对象与对象创建方法的各种方法
2019/02/27 Javascript
JS数组中对象去重操作示例
2019/06/04 Javascript
VUE路由动态加载实例代码讲解
2019/08/26 Javascript
webpack + vue 打包生成公共配置文件(域名) 方便动态修改
2019/08/29 Javascript
深入Python解释器理解Python中的字节码
2015/04/01 Python
python通过apply使用元祖和列表调用函数实例
2015/05/26 Python
在Django中进行用户注册和邮箱验证的方法
2016/05/09 Python
python用户管理系统的实例讲解
2017/12/23 Python
Python爬虫beautifulsoup4常用的解析方法总结
2019/02/25 Python
详解python tkinter教程-事件绑定
2019/03/28 Python
Python 3.8中实现functools.cached_property功能
2019/05/29 Python
python实现QQ批量登录功能
2019/06/19 Python
python中tkinter窗口位置\坐标\大小等实现示例
2020/07/09 Python
HTML5 progress和meter控件_动力节点Java学院整理
2017/07/06 HTML / CSS
群胜软件Java笔试题
2012/09/29 面试题
舞蹈比赛获奖感言
2014/02/04 职场文书
书香校园活动方案
2014/02/28 职场文书
小学优秀教育工作者事迹材料
2014/05/09 职场文书
乌镇导游词
2015/02/02 职场文书
建筑工程材料员岗位职责
2015/04/11 职场文书
教师师德承诺书2016
2016/03/25 职场文书
Python实战之用tkinter库做一个鼠标模拟点击器
2021/04/27 Python
《艾尔登法环》发布最新「战技」宣传片
2022/04/03 其他游戏