1. # -*- coding: utf-8 -*-
    """
    some function by metaphy,2007-04-03,copyleft
    version 0.2
    """
    import urllib, httplib, urlparse
    import re
    import random
  2.  
  3. """judge url exists or not,by others"""
    def httpExists(url):
    host, path = urlparse.urlsplit(url)[1:3]
    if ':' in host:
    # port specified, try to use it
    host, port = host.split(':', 1)
    try:
    port = int(port)
    except ValueError:
    print 'invalid port number %r' % (port,)
    return False
    else:
    # no port specified, use default port
    port = None
    try:
    connection = httplib.HTTPConnection(host, port=port)
    connection.request("HEAD", path)
    resp = connection.getresponse( )
    if resp.status == 200: # normal 'found' status
    found = True
    elif resp.status == 302: # recurse on temporary redirect
    found = httpExists(urlparse.urljoin(url,resp.getheader('location', '')))
    else: # everything else -> not found
    print "Status %d %s : %s" % (resp.status, resp.reason, url)
    found = False
    except Exception, e:
    print e.__class__, e, url
    found = False
    return found
  4.  
  5. """get html src,return lines[]"""
    def gGetHtmlLines(url):
    if url==None : return
    if not httpExists(url): return
    try:
    page = urllib.urlopen(url)
    html = page.readlines()
    page.close()
    return html
    except:
    print "gGetHtmlLines() error!"
    return
    """get html src,return string"""
    def gGetHtml(url):
    if url==None : return
    if not httpExists(url): return
    try:
    page = urllib.urlopen(url)
    html = page.read()
    page.close()
    return html
    except:
    print "gGetHtml() error!"
    return
  6.  
  7. """根据url获取文件名"""
    def gGetFileName(url):
    if url==None: return None
    if url=="" : return ""
    arr=url.split("/")
    return arr[len(arr)-1]
  8.  
  9. """生成随机文件名"""
    def gRandFilename(type):
    fname = ''
    for i in range(16):
    fname = fname + chr(random.randint(65,90))
    fname = fname + chr(random.randint(48,57))
    return fname + '.' + type
    """根据url和其上的link,得到link的绝对地址"""
    def gGetAbslLink(url,link):
    if url==None or link == None : return
    if url=='' or link=='' : return url
    addr = ''
    if link[0] == '/' :
    addr = gGetHttpAddr(url) + link
    elif len(link)>3 and link[0:4] == 'http':
    addr = link
    elif len(link)>2 and link[0:2] == '..':
    addr = gGetHttpAddrFatherAssign(url,link)
    else:
    addr = gGetHttpAddrFather(url) + link
  10.  
  11. return addr
  12.  
  13. """根据输入的lines,匹配正则表达式,返回list"""
    def gGetRegList(linesList,regx):
    if linesList==None : return
    rtnList=[]
    for line in linesList:
    matchs = re.search(regx, line, re.IGNORECASE)
    if matchs!=None:
    allGroups = matchs.groups()
    for foundStr in allGroups:
    if foundStr not in rtnList:
    rtnList.append(foundStr)
    return rtnList
    """根据url下载文件,文件名参数指定"""
    def gDownloadWithFilename(url,savePath,file):
    #参数检查,现忽略
    try:
    urlopen=urllib.URLopener()
    fp = urlopen.open(url)
    data = fp.read()
    fp.close()
    file=open(savePath + file,'w+b')
    file.write(data)
    file.close()
    except IOError:
    print "download error!"+ url
  14.  
  15. """根据url下载文件,文件名自动从url获取"""
    def gDownload(url,savePath):
    #参数检查,现忽略
    fileName = gGetFileName(url)
    #fileName =gRandFilename('jpg')
    gDownloadWithFilename(url,savePath,fileName)
  16.  
  17. """根据某网页的url,下载该网页的jpg"""
    def gDownloadHtmlJpg(downloadUrl,savePath):
    lines= gGetHtmlLines(downloadUrl)
    regx = r"""src\s*="?(\S+)\.jpg"""
    lists =gGetRegList(lines,regx)
    if lists==None: return
    for jpg in lists:
    jpg = gGetAbslLink(downloadUrl,jpg) + '.jpg'
    gDownload(jpg,savePath)
    ### print gGetFileName(jpg)
    """根据url取主站地址"""
    def gGetHttpAddr(url):
    if url== '' : return ''
    arr=url.split("/")
    return arr[0]+"//"+arr[2]
    """根据url取上级目录"""
    def gGetHttpAddrFather(url):
    if url=='' : return ''
    arr=url.split("/")
    addr = arr[0]+'//'+arr[2]+ '/'
    if len(arr)-1>3 :
    for i in range(3,len(arr)-1):
    addr = addr + arr[i] + '/'
    return addr
  18.  
  19. """根据url和上级的link取link的绝对地址"""
    def gGetHttpAddrFatherAssign(url,link):
    if url=='' : return ''
    if link=='': return ''
    linkArray=link.split("/")
    urlArray = url.split("/")
    partLink =''
    partUrl = ''
    for i in range(len(linkArray)):
    if linkArray[i]=='..':
    numOfFather = i + 1 #上级数
    else:
    partLink = partLink + '/' + linkArray[i]
    for i in range(len(urlArray)-1-numOfFather):
    partUrl = partUrl + urlArray[i]
    if i < len(urlArray)-1-numOfFather -1 :
    partUrl = partUrl + '/'
    return partUrl + partLink
  20.  
  21. """根据url获取其上的相关htm、html链接,返回list"""
    def gGetHtmlLink(url):
    #参数检查,现忽略
    rtnList=[]
    lines=gGetHtmlLines(url)
    regx = r"""href="?(\S+)\.htm"""
    for link in gGetRegList(lines,regx):
    link = gGetAbslLink(url,link) + '.htm'
    if link not in rtnList:
    rtnList.append(link)
    print link
    return rtnList
  22.  
  23. """根据url,抓取其上的jpg和其链接htm上的jpg"""
    def gDownloadAllJpg(url,savePath):
    #参数检查,现忽略
    gDownloadHtmlJpg(url,savePath)
    #抓取link上的jpg
    links=gGetHtmlLink(url)
    for link in links:
    gDownloadHtmlJpg(link,savePath)
  24.  
  25. """test"""
    def test():
    u='http://www.gs.xinhuanet.com/news/2007-01/31/content_9188207_1.htm'
    save='d:/tmp/'
    print 'download pic from [' + u +']'
    print 'save to [' +save+'] ...'
    gDownloadHtmlJpg(u,save)
    print "download finished"
  26.  
  27. test()

python下载图片(3)的更多相关文章

  1. python下载图片超时的调查

    在使用python3下载图片时, 常用的方法有urlretrieve和requests两种, 不管哪种方法在网速极慢的情况下, 会出现图片下载卡住现象.那如何解决呢? 小编根据网上提供的资料测试了几种 ...

  2. Python下载图片并保存本地的两种方式

    一:使用Python中的urllib类中的urlretrieve()函数,直接从网上下载资源到本地,具体代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...

  3. python下载图片(2)

    #-*- coding: UTF-8 -*- import urllib2, re,datetime,time, os,sys from PIL import Image, ImageDraw, Im ...

  4. python 下载图片的方法

    a='http://wx1.sinaimg.cn/mw600/006HOayNgy1fqjdi2nxohj32pw3o8x6s.jpg'  #图片下载地址   ( 这里改成 文件txt地址)w='/U ...

  5. 使用python下载图片(福利)

    刚学python 没多久, 代码处处是漏洞,也希望各位大佬理解一下 爬出来的图片... 使用的 是 https://www.tianapi.com/  接口下的 美女图片... (需要自己注册一个账号 ...

  6. Python下载图片小程序

    欢迎大侠们指正批评 思路: 1.引入相关的python文件(import re  import urllib) 2.读取对应网页的html文件(使用 urllib) def getHtml(url): ...

  7. Python 下载图片的几种方法

    import osos.makedirs('./image/', exist_ok=True)IMAGE_URL = "http://image.nationalgeographic.com ...

  8. python下载图片

    import re import  urllib.request   def getHtml(url): page = urllib.request.urlopen(url) html = page. ...

  9. python 下载图片

    import requests from PIL import Image from io import BytesIO url = 'http://image2.buslive.cn/shp/upl ...

随机推荐

  1. Mybatis全面详解——下(学习总结)

    原文地址:https://blog.csdn.net/ititii/article/details/79999481 一.Mybatis关联查询映射 这里采用一个案例来进行分析:(下面的案例都是采用M ...

  2. MyCAT分表初体验

    1.mycat二进制包安装 下载地址:http://dl.mycat.io/ tar -zxvf Mycat-server-1.6.5-release-20180122220033-linux.tar ...

  3. 我的前端规范——CSS篇

    相关文章 简书原文:https://www.jianshu.com/p/e87bfd27ff59 我的前端规范——开篇:http://www.cnblogs.com/shcrk/p/9271561.h ...

  4. angular管道相关知识

    原文地址 https://www.jianshu.com/p/22e0f95bcf24 什么是管道 每个应用开始的时候差不多都是一些简单任务:获取数据.转换它们,然后把它们显示给用户. 获取数据可能简 ...

  5. asm 的hello world 2011.04.28

    这几天一直在弄一个嵌入式的程序,搭环境,熟悉库函数,熟悉汇编,乱成一锅粥,到现在还是没有什么系统性的收获. 或许下周弄出来吧,(一定得弄出来,不然老大该跟我急了……). 今天,熟悉汇编,好歹用汇编写出 ...

  6. 蓝牙简单配对(Simple Pairing)协议及代码流程简述

    kangear注: 文章转自:http://blog.csdn.net/myxmu/article/details/12217135 原文把图给搞丢了.可是文章太好了,这个时候我就发挥多年的Googl ...

  7. Fragment使用LocalBroadcastManager接收广播消息

    这种方式不用在配置文件加东西 变量声明 LocalBroadcastManager broadcastManager; IntentFilter intentFilter; BroadcastRece ...

  8. 【codeforces 758C】Unfair Poll

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. 善用Linux与Windows中的筛选功能及其他有用功能

    cmd中的检索目录结构是用 tree命令,检索本目录中这一级别的所有文件是dir,要是文件很多时需要用到检索功能 dir | find "abc" #####主要find之后要加双 ...

  10. java基础——try catch final

    1.不管有木有出现异常,finally块中代码都会执行: 2.当try和catch中有return时,finally仍然会执行: 3.finally是在return后面的表达式运算后执行的(此时并没有 ...