最近老師讓學習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_1.html)
[?http://pic.yxdown.com/list/0_0_75.html](http://pic.yxdown.com/list/0_0_1.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*******\n\n')
#建議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)+'頁*******\n\n')
#爬取對應主題的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+'\n')
#獲取每頁所有主題對應的URL并輸出
if "_blank" in line: #防止獲取列表list/1_0_1.html list/2_0_1.html
#獲取主題
fileurl.write('\n\n**********************\n')
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+'\n')
#獲取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)+'\n') #形如"/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)+'\n')
'''
#第四步 下載圖片
#如果瀏覽器存在驗證信息如維基百科 需添加如下代碼
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](http://pic.yxdown.com/list/0_0_1.html)的圖片E:\\Picture文件夾如下:

下載[http://pic.yxdown.com/list/0_0_3.html](http://pic.yxdown.com/list/0_0_1.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](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/](http://blog.csdn.net/eastmount/))
- 前言
- [Python學習] 專題一.函數的基礎知識
- [Python學習] 專題二.條件語句和循環語句的基礎知識
- [Python學習] 專題三.字符串的基礎知識
- [Python學習] 簡單網絡爬蟲抓取博客文章及思想介紹
- [Python學習] 專題四.文件基礎知識
- [python學習] 簡單爬取維基百科程序語言消息盒
- [python學習] 簡單爬取圖片網站圖庫中圖片
- [python知識] 爬蟲知識之BeautifulSoup庫安裝及簡單介紹
- [python+nltk] 自然語言處理簡單介紹和NLTK壞境配置及入門知識(一)
- [python學習] 模仿瀏覽器下載CSDN源文并實現PDF格式備份
- [Python學習] 簡單爬取CSDN下載資源信息
- [Python] 專題五.列表基礎知識 二維list排序、獲取下標和處理txt文本實例
- [Python學習] 專題六.局部變量、全局變量global、導入模塊變量
- [python] 專題七.網絡編程之套接字Socket、TCP和UDP通信實例
- [python] 專題八.多線程編程之thread和threading