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

来源:互联网 发布:神鬼传奇怪物数据 编辑:程序博客网 时间:2024/05/22 00:16
        最近老师让学习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文件夹)
[python] view plaincopy在CODE上查看代码片派生到我的代码片
  1. # coding=utf-8  
  2. # 声明编码方式 默认编码方式ASCII 参考https://www.python.org/dev/peps/pep-0263/  
  3. import urllib  
  4. import time  
  5. import re  
  6. import os  
  7.   
  8. ''''' 
  9. Python下载游迅网图片 BY:Eastmount 
  10. '''  
  11.   
  12. ''''' 
  13. ************************************************** 
  14. #第一步 遍历获取每页对应主题的URL 
  15. #http://pic.yxdown.com/list/0_0_1.html 
  16. #http://pic.yxdown.com/list/0_0_75.html 
  17. ************************************************** 
  18. '''  
  19. fileurl=open('yxdown_url.txt','w')  
  20. fileurl.write('****************获取游讯网图片URL*************\n\n')   
  21. #建议num=3 while num<=3一次遍历一个页面所有主题,下次换成num=4 while num<=4而不是1-75   
  22. num=3  
  23. while num<=3:  
  24.     temp = 'http://pic.yxdown.com/list/0_0_'+str(num)+'.html'  
  25.     content = urllib.urlopen(temp).read()  
  26.     open('yxdown_'+str(num)+'.html','w+').write(content)  
  27.     print temp  
  28.     fileurl.write('****************第'+str(num)+'页*************\n\n')  
  29.   
  30.     #爬取对应主题的URL  
  31.     #<div class="cbmiddle"></div>中<a target="_blank" href="/html/5533.html" >  
  32.     count=1 #计算每页1-75中具体网页个数  
  33.     res_div = r'<div class="cbmiddle">(.*?)</div>'   
  34.     m_div = re.findall(res_div,content,re.S|re.M)  
  35.     for line in m_div:  
  36.         #fileurl.write(line+'\n')  
  37.         #获取每页所有主题对应的URL并输出  
  38.         if "_blank" in line: #防止获取列表list/1_0_1.html list/2_0_1.html  
  39.             #获取主题  
  40.             fileurl.write('\n\n********************************************\n')  
  41.             title_pat = r'<b class="imgname">(.*?)</b>'  
  42.             title_ex = re.compile(title_pat,re.M|re.S)  
  43.             title_obj = re.search(title_ex, line)  
  44.             title = title_obj.group()  
  45.             print unicode(title,'utf-8')  
  46.             fileurl.write(title+'\n')  
  47.             #获取URL  
  48.             res_href = r'<a target="_blank" href="(.*?)"'  
  49.             m_linklist = re.findall(res_href,line)  
  50.             #print unicode(str(m_linklist),'utf-8')  
  51.             for link in m_linklist:  
  52.                 fileurl.write(str(link)+'\n'#形如"/html/5533.html"  
  53.   
  54.                 ''''' 
  55.                 ************************************************** 
  56.                 #第二步 去到具体图像页面 下载HTML页面 
  57.                 #http://pic.yxdown.com/html/5533.html#p=1 
  58.                 #注意先本地创建yxdown 否则报错No such file or directory 
  59.                 ************************************************** 
  60.                 '''  
  61.                 #下载HTML网页无原图 故加'#p=1'错误  
  62.                 #HTTP Error 400. The request URL is invalid.  
  63.                 html_url = 'http://pic.yxdown.com'+str(link)  
  64.                 print html_url  
  65.                 html_content = urllib.urlopen(html_url).read() #具体网站内容  
  66.                 #可注释它 暂不下载静态HTML  
  67.                 open('yxdown/yxdown_html'+str(count)+'.html','w+').write(html_content)  
  68.   
  69.   
  70.                 ''''' 
  71.                 #第三步 去到图片界面下载图片 
  72.                 #图片的链接地址为http://pic.yxdown.com/html/5530.html#p=1 #p=2 
  73.                 #点击"查看原图"HTML代码如下 
  74.                 #<a href="javascript:;" style=""onclick="return false;">查看原图</a> 
  75.                 #通过JavaScript实现 而且该界面存储所有图片链接<script></script>之间 
  76.                 #获取"original":"http://i-2.yxdown.com/2015/3/18/6381ccc..3158d6ad23e.jpg" 
  77.                 '''  
  78.                 html_script = r'<script>(.*?)</script>'  
  79.                 m_script = re.findall(html_script,html_content,re.S|re.M)  
  80.                 for script in m_script:  
  81.                     res_original = r'"original":"(.*?)"' #原图  
  82.                     m_original = re.findall(res_original,script)  
  83.                     for pic_url in m_original:  
  84.                         print pic_url  
  85.                         fileurl.write(str(pic_url)+'\n')  
  86.   
  87.                         ''''' 
  88.                         #第四步 下载图片 
  89.                         #如果浏览器存在验证信息如维基百科 需添加如下代码 
  90.                             class AppURLopener(urllib.FancyURLopener): 
  91.                                 version = "Mozilla/5.0" 
  92.                             urllib._urlopener = AppURLopener() 
  93.                         #参考 http://bbs.csdn.net/topics/380203601 
  94.                         #http://www.lylinux.org/python使用多线程下载图片.html 
  95.                         '''  
  96.                         filename = os.path.basename(pic_url) #去掉目录路径,返回文件名  
  97.                         #No such file or directory 需要先创建文件Picture3  
  98.                         urllib.urlretrieve(pic_url, 'E:\\Picture3\\'+filename)  
  99.                         #http://pic.yxdown.com/html/5519.html  
  100.                         #IOError: [Errno socket error] [Errno 10060]   
  101.                   
  102.                 #只输出一个URL 否则输出两个相同的URL  
  103.                 break   
  104.               
  105.             #当前页具体内容个数加1  
  106.             count=count+1  
  107.             time.sleep(0.1)    
  108.         else:  
  109.             print 'no url about content'  
  110.           
  111.     time.sleep(1)    
  112.     num=num+1  
  113. else:  
  114.     print 'Download Over!!!'  
  115.       
        其中下载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] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <!-- 第一步 爬取的HTML代码如下 -->  
  2. <div class="conbox">  
  3.   <div class="cbtop">  
  4.   </div>  
  5.   <div class="cbmiddle">  
  6.   <a target="_blank" href="/html/5533.html" class="proimg">  
  7.     <img src="http://i-2.yxdown.com/2015/3/19/KDE5Mngp/a78649d0-9902-4086-a274-49f9f3015d96.jpg" alt="Miss大小姐驾到!细数《英雄联盟》圈的电竞女神" />  
  8.     <strong></strong>  
  9.     <p>  
  10.       <span>b></b>1836人看过</span>  
  11.       <em><b></b>10张</em>  
  12.     </p>  
  13.     <b class="imgname">Miss大小姐驾到!细数《英雄联盟》圈的电竞女神</b>  
  14.   </a>  
  15.   <a target="_blank" href="/html/5533.html" class="plLink"><em>1</em>人评论</a>  
  16.   </div>  
  17.   <div class="cbbottom">  
  18.   </div>  
  19.   <a target="_blank" class="plBtn" href="/html/5533.html"></a>  
  20. </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>中:
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <script>var images = [  
  2. { "big":"http://i-2.yxdown.com/2015/3/18/KDkwMHgp/6381ccc0-ed65-4422-8671-b3158d6ad23e.jpg",  
  3.   "thumb":"http://i-2.yxdown.com/2015/3/18/KHgxMjAp/6381ccc0-ed65-4422-8671-b3158d6ad23e.jpg",  
  4.   "original":"http://i-2.yxdown.com/2015/3/18/6381ccc0-ed65-4422-8671-b3158d6ad23e.jpg",  
  5.   "title":"","descript":"","id":75109},  
  6. { "big":"http://i-2.yxdown.com/2015/3/18/KDkwMHgp/fec26de9-8727-424a-b272-f2827669a320.jpg",  
  7.   "thumb":"http://i-2.yxdown.com/2015/3/18/KHgxMjAp/fec26de9-8727-424a-b272-f2827669a320.jpg",  
  8.   "original":"http://i-2.yxdown.com/2015/3/18/fec26de9-8727-424a-b272-f2827669a320.jpg",  
  9.   "title":"","descript":"","id":75110},  
  10. ...  
  11. </script>  
        其中获取原图-original即可,缩略图-thumb,大图-big,通过正则表达式下载URL:
        res_original = r'"original":"(.*?)"' #原图
        m_original = re.findall(res_original,script)
        4.最后一步就是下载图片,其中我不太会使用线程,只是简单添加了time.sleep(0.1) 函数。下载图片可能会遇到维基百科那种访问受限,需要相应设置,核心代码如下:
[python] view plaincopy在CODE上查看代码片派生到我的代码片
  1. import os  
  2. import urllib  
  3. class AppURLopener(urllib.FancyURLopener):  
  4.     version = "Mozilla/5.0"  
  5. urllib._urlopener = AppURLopener()  
  6. url = "http://i-2.yxdown.com/2015/2/25/c205972d-d858-4dcd-9c8b-8c0f876407f8.jpg"  
  7. filename = os.path.basename(url)  
  8. urllib.urlretrieve(url , filename)  
       同时我也在本地创建文件夹Picture3,并txt记录获取的URL,如下图所示:

        最后希望文章对大家有所帮助,简单来说文章就两句话:如何分析源代码通过正则表达式提取指定URL;如何通过Python下载图片。如果文章有不足之处,请海涵!
     (By:Eastmount 2015-3-20 下午5点  http://blog.csdn.net/eastmount/)
0 0
原创粉丝点击