【爬蟲】碼了個畢業照爬蟲,畢業照爬蟲

來源:互聯網
上載者:User

【爬蟲】碼了個畢業照爬蟲,畢業照爬蟲

怪不得都說壓力山大,連山大伺服器壓力都太大了。並且編輯直接把圖片掛到一個頁面上是什麼意思= =,一張照片8M+,反正外網速度台有限了。索性寫個爬蟲讓他慢慢下吧,順便當學習練手了。。。(PS:不知道為什麼在windows下面在頁面中用迅雷下載全部連結也無效,不知道什麼原因?)

一共192組圖片,前20組由於網頁上順序有問題,後期爬蟲寫完後又懶得改正則匹配了,所以就這樣吧_(:з」∠)_


代碼如下,一句句疊上來的,再簡單不過了:

有python環境的可以自己直接儲存後跑一下,不感興趣的直接百度網盤下載吧

http://pan.baidu.com/s/1hSvH8

(感覺用sublimeText安裝python支援後直接F5沒有在linux下直接python 檔案名稱速度快,不知道是不是因為測試時間不同情況下網速差異造成的)

#!/usr/bin/env python#! -*- coding: utf-8 -*-import urllib,urllib2import reimport time#返回網頁原始碼def getHtml(url):# print 'Start Downloading Html Source Code'user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'headers = {'User-Agent' : user_agent }req = urllib2.Request(url,headers = headers)html = urllib2.urlopen(req)srcCode = html.read()#print srcCodereturn srcCode#srcCode 包含圖片的網頁def getImg(srcCode,startNum,endNum):#對網頁中圖片建立正則,並存為listpattern = re.compile(r'<a.*?href="(.*?)" title="第.*?組.JPG">')imgSrcHtml = pattern.findall(srcCode)print imgSrcHtml#print len(imgSrcHtml)num = startNum# count = endPage - startPage# for x in xrange(1,count):for i in imgSrcHtml[startNum-1:endNum-1]:# 補全連結,得到完整地址i = 'http://www.online.sdu.edu.cn' + i#print i# return imageRealSrc# for src in imageRealSrc:# urllib.urlretrieve(src,'%s.jpg' % num)user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'headers = {'User-Agent' : user_agent }req = urllib2.Request(i,headers = headers)print "Downloading..."print iprint "and saving as 第%s組.jpg\n" %(num+8)##html = urllib2.urlopen(req,timeout = 5)html = urllib2.urlopen(req)f = open("./pics/" + u'第%s組.jpg' % (num+8), 'w+b')f.write(html.read())f.close()num += 1# try:# html = urllib2.urlopen(req,timeout = 5)# except Exception, e:# print '拋出異常為:' + str(e)# break# try:# f = open("./" + '%s.jpg' % num, 'w+b')# f.write(html.read())# f.close()# num += 1# except Exception, e:# print '拋出異常為:' + str(e)# # num +=1# break        # time.sleep(5)print '全部任務完成!'ImgUrl = "http://www.online.sdu.edu.cn/news/article-17317.html"print "共有184張照片,請依次輸入開始數和結束數"start = int(raw_input("Start number\n"))end = int(raw_input("End number\n"))print "Starting..."getImg(getHtml(ImgUrl),start,end)




著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.