Python學習之簡單網頁爬蟲

來源:互聯網
上載者:User

標籤:adl   案例   readline   \n   ref   body   簡單   lin   爬取   

0x00案例   爬取部落格園某頁面的所有圖片並下載到本地

連續搞了一周的python,收穫頗多,主要還是鍛煉了自己的脾氣。。。話不多說,先貼上指令碼

#coding:utf-8import urllib2import reurl="https://www.cnblogs.com/peterpan0707007/p/7620048.html"headers={‘User-Agent‘:‘Mozilla/5.0 (Windows NT 10.0; WOW64; rv:59.0) Gecko/20100101 Firefox/59.0‘}req=urllib2.Request(url,headers=headers)resp=urllib2.urlopen(req).read()data=re.compile(r‘http:.+\.png‘).findall(resp)  #正則匹配png圖片#print datafor i in data:    with open(‘result.txt‘,‘a‘) as fw:        fw.write(i+‘\n‘)        with open(‘result.txt‘,‘r‘) as fr:    for line in fr.readlines():        response=urllib2.urlopen(line).read()        filename=line.strip(‘\n‘).split(‘-‘)[2]+‘.png‘        with open(filename,‘wb‘) as fw:            fw.write(response)    print ‘done‘

運行結果

 

Python學習之簡單網頁爬蟲

聯繫我們

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