• [python学习] 简单爬取图片站点图库中图片


            近期老师让学习Python与维基百科相关的知识,无聊之中用Python简单做了个爬取“游讯网图库”中的图片,由于每次点击下一张感觉很浪费时间又繁琐。主要分享的是怎样爬取HTML的知识和Python怎样下载图片。希望对大家有所帮助,同一时候发现该站点的图片都挺精美的,建议阅读原网下载图片,支持游讯网不要去破坏它。
            通过浏览游讯网发现它的图库URL为。当中所有图片为0_0_1到0_0_75:
            http://pic.yxdown.com/list/0_0_1.html
            http://pic.yxdown.com/list/0_0_75.html
            同一时候通过下图能够发现游讯网的1-75页个列表,每页中有非常多个主题。每一个主题都有对应的多张图片。


            源码例如以下:
            (需在本地创建E:\Picture3目录和Python执行目录创建yxdown目录)
    # coding=utf-8
    # 声明编码方式 默认编码方式ASCII 參考https://www.python.org/dev/peps/pep-0263/
    import urllib
    import time
    import re
    import os
    
    '''
    Python下载游迅网图片 BY:Eastmount
    '''
    
    '''
    **************************************************
    #第一步 遍历获取每页相应主题的URL
    #http://pic.yxdown.com/list/0_0_1.html
    #http://pic.yxdown.com/list/0_0_75.html
    **************************************************
    '''
    fileurl=open('yxdown_url.txt','w')
    fileurl.write('****************获取游讯网图片URL*************
    
    ') 
    #建议num=3 while num<=3一次遍历一个页面全部主题,下次换成num=4 while num<=4而不是1-75 
    num=3
    while num<=3:
        temp = 'http://pic.yxdown.com/list/0_0_'+str(num)+'.html'
        content = urllib.urlopen(temp).read()
        open('yxdown_'+str(num)+'.html','w+').write(content)
        print temp
        fileurl.write('****************第'+str(num)+'页*************
    
    ')
    
        #爬取相应主题的URL
        #<div class="cbmiddle"></div>中<a target="_blank" href="/html/5533.html" >
        count=1 #计算每页1-75中详细网页个数
        res_div = r'<div class="cbmiddle">(.*?

    )</div>' m_div = re.findall(res_div,content,re.S|re.M) for line in m_div: #fileurl.write(line+' ') #获取每页全部主题相应的URL并输出 if "_blank" in line: #防止获取列表list/1_0_1.html list/2_0_1.html #获取主题 fileurl.write(' ******************************************** ') title_pat = r'<b class="imgname">(.*?

    )</b>' title_ex = re.compile(title_pat,re.M|re.S) title_obj = re.search(title_ex, line) title = title_obj.group() print unicode(title,'utf-8') fileurl.write(title+' ') #获取URL res_href = r'<a target="_blank" href="(.*?

    )"' m_linklist = re.findall(res_href,line) #print unicode(str(m_linklist),'utf-8') for link in m_linklist: fileurl.write(str(link)+' ') #形如"/html/5533.html" ''' ************************************************** #第二步 去到详细图像页面 下载HTML页面 #http://pic.yxdown.com/html/5533.html#p=1 #注意先本地创建yxdown 否则报错No such file or directory ************************************************** ''' #下载HTML网页无原图 故加'#p=1'错误 #HTTP Error 400. The request URL is invalid. html_url = 'http://pic.yxdown.com'+str(link) print html_url html_content = urllib.urlopen(html_url).read() #详细站点内容 #可凝视它 暂不下载静态HTML open('yxdown/yxdown_html'+str(count)+'.html','w+').write(html_content) ''' #第三步 去到图片界面下载图片 #图片的链接地址为http://pic.yxdown.com/html/5530.html#p=1 #p=2 #点击"查看原图"HTML代码例如以下 #<a href="javascript:;" style=""onclick="return false;">查看原图</a> #通过JavaScript实现 并且该界面存储全部图片链接<script></script>之间 #获取"original":"http://i-2.yxdown.com/2015/3/18/6381ccc..3158d6ad23e.jpg" ''' html_script = r'<script>(.*?

    )</script>' m_script = re.findall(html_script,html_content,re.S|re.M) for script in m_script: res_original = r'"original":"(.*?

    )"' #原图 m_original = re.findall(res_original,script) for pic_url in m_original: print pic_url fileurl.write(str(pic_url)+' ') ''' #第四步 下载图片 #假设浏览器存在验证信息如维基百科 需加入例如以下代码 class AppURLopener(urllib.FancyURLopener): version = "Mozilla/5.0" urllib._urlopener = AppURLopener() #參考 http://bbs.csdn.net/topics/380203601 #http://www.lylinux.org/python使用多线程下载图片.html ''' filename = os.path.basename(pic_url) #去掉文件夹路径,返回文件名称 #No such file or directory 须要先创建文件Picture3 urllib.urlretrieve(pic_url, 'E:\Picture3\'+filename) #http://pic.yxdown.com/html/5519.html #IOError: [Errno socket error] [Errno 10060] #仅仅输出一个URL 否则输出两个同样的URL break #当前页详细内容个数加1 count=count+1 time.sleep(0.1) else: print 'no url about content' time.sleep(1) num=num+1 else: print 'Download Over!!!'

            当中下载http://pic.yxdown.com/list/0_0_1.html的图片E:\Picture目录例如以下:


            下载http://pic.yxdown.com/list/0_0_3.html的图片E:\Picture3目录例如以下:


           因为代码凝视中有具体的步骤。以下仅仅是简介过程。


           1.简单遍历站点。获取每页相应主题的URL。当中每页都有无数个主题。当中主题的格式例如以下:

    <!-- 第一步 爬取的HTML代码例如以下 -->
    <div class="conbox">
      <div class="cbtop">
      </div>
      <div class="cbmiddle">
      <a target="_blank" href="/html/5533.html" class="proimg">
        <img src="http://i-2.yxdown.com/2015/3/19/KDE5Mngp/a78649d0-9902-4086-a274-49f9f3015d96.jpg" alt="Miss大小姐驾到!

    细数《英雄联盟》圈的电竞女神" /> <strong></strong> <p> <span>b></b>1836人看过</span> <em><b></b>10张</em> </p> <b class="imgname">Miss大小姐驾到!

    细数《英雄联盟》圈的电竞女神</b> </a> <a target="_blank" href="/html/5533.html" class="plLink"><em>1</em>人评论</a> </div> <div class="cbbottom"> </div> <a target="_blank" class="plBtn" href="/html/5533.html"></a> </div>

            它是由无数个<div class="conbox"></div>组成,当中我们仅仅须要提取<a target="_blank" href="/html/5533.html" class="proimg">中的href就可以,然后通过URL拼接实现到详细的主题页面。当中相应上面的布局例如以下图所看到的:
      
            2.去到详细图像页面 下载HTML页面。如:
            http://pic.yxdown.com/html/5533.html#p=1
            同一时候下载本地HTML页面能够凝视该句代码。此时须要点击“查看图片”才干下载原图。点击右键仅仅能另存为站点html。
            3.我最初打算是是分析“查看原图”的URL来实现下载,其它站点同理是分析“下一页”来实现的。

    但我发现它是通过JavaScript实现的浏览,即:
            <a href="javascript:;" onclick="return false;" id="photoOriginal">查看原图</a>
            同一时候它把全部图片都写在以下代码<script></script>中:

    <script>var images = [
    { "big":"http://i-2.yxdown.com/2015/3/18/KDkwMHgp/6381ccc0-ed65-4422-8671-b3158d6ad23e.jpg",
      "thumb":"http://i-2.yxdown.com/2015/3/18/KHgxMjAp/6381ccc0-ed65-4422-8671-b3158d6ad23e.jpg",
      "original":"http://i-2.yxdown.com/2015/3/18/6381ccc0-ed65-4422-8671-b3158d6ad23e.jpg",
      "title":"","descript":"","id":75109},
    { "big":"http://i-2.yxdown.com/2015/3/18/KDkwMHgp/fec26de9-8727-424a-b272-f2827669a320.jpg",
      "thumb":"http://i-2.yxdown.com/2015/3/18/KHgxMjAp/fec26de9-8727-424a-b272-f2827669a320.jpg",
      "original":"http://i-2.yxdown.com/2015/3/18/fec26de9-8727-424a-b272-f2827669a320.jpg",
      "title":"","descript":"","id":75110},
    ...
    </script>
            当中获取原图-original就可以,缩略图-thumb,大图-big,通过正則表達式下载URL:
            res_original = r'"original":"(.*?)"' #原图
            m_original = re.findall(res_original,script)
            4.最后一步就是下载图片。当中我不太会使用线程,仅仅是简单加入了time.sleep(0.1) 函数。下载图片可能会遇到维基百科那种訪问受限。须要对应设置。核心代码例如以下:
    import os
    import urllib
    class AppURLopener(urllib.FancyURLopener):
        version = "Mozilla/5.0"
    urllib._urlopener = AppURLopener()
    url = "http://i-2.yxdown.com/2015/2/25/c205972d-d858-4dcd-9c8b-8c0f876407f8.jpg"
    filename = os.path.basename(url)
    urllib.urlretrieve(url , filename)
           同一时候我也在本地创建目录Picture3,并txt记录获取的URL,例如以下图所看到的:

            最后希望文章对大家有所帮助,简单来说文章就两句话:怎样分析源码通过正則表達式提取指定URL。怎样通过Python下载图片。假设文章有不足之处,请海涵!


         (By:Eastmount 2015-3-20 下午5点  http://blog.csdn.net/eastmount/



  • 相关阅读:
    ASP.NET中JSON的序列化和反序列化
    Android:数据存储之SQLite
    转Android:简单联网获取网页代码
    Android:@id和@+id
    linux .run文件安装
    Mysql的Root密码忘记,查看或修改的解决方法(图文介绍)
    网页页面尺寸
    openstack
    br0
    virsh
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/7249681.html
Copyright © 2020-2023  润新知