頭像Python爬蟲1.0__Python

來源:互聯網
上載者:User

懶得說明了,程式中注釋的差不多了。該代碼只提供學習,為了避免使用不當,引起不必要麻煩,把代碼中的Url刪掉。

# -*- coding:utf-8 -*-#------------------------------------------#   程式:美女頭像爬蟲#   版本:1.0#   作者:執劍天涯#   日期:2016-08-14#   版本:Python 2.7.7#   操作:無#   功能:載入100頁MM的頭像#   思想:面向過程#------------------------------------------import urllibimport urllib2import reimport randomimport ospageIndex = 1  #頁數中間變數num = 0  #每頁頭像個數變數print u'正在儲存圖片...'for i in range(0,100):    Url = '網站的Url'+str(pageIndex)    pageIndex += 1    request = urllib2.Request(Url)    response = urllib2.urlopen(request)    page = response.read().decode('gbk')    #print page    patternName = re.compile('<a class="lady-name".*?>(.*?)</a>',re.S)    names = re.findall(patternName,page)    #for name in names:        #print name    patternImg = re.compile('<img src="(.*?)"',re.S)    imgs = re.findall(patternImg,page)        for img in imgs:        img = 'https:'+img  #圖片Url        img = urllib.urlopen(img)          data = img.read()  #讀取圖片        path = 'D:\\mmPictures'  #檔案夾路徑        #判斷路徑是否存在,不存在就建立路徑        isExists = os.path.exists(path)        if not isExists:            os.makedirs(path)        else:            pass        fileName = 'D:\\mmPictures\%s.jpg' %(names[num])  #MM名字作為檔案名稱字        print names[num]        num += 1                f = open(fileName,'wb')  #建立檔案        f.write(data)  #寫進圖片        f.close  #關閉檔案    num = 0print u'儲存完成。'


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.