Python學習---網頁爬蟲[下載圖片]

來源:互聯網
上載者:User

標籤:pytho   odi   none   正則匹配   http   str   tool   one   otto   

爬蟲學習--下載圖片

1.主要用到了urllib和re庫

2.利用urllib.urlopen()函數獲得頁面原始碼

3.利用正則匹配圖片類型,當然正則越準確,下載的越多

4.利用urllib.urlretrieve()下載圖片,並且可以重新命名,利用%S

5.應該是電訊廠商有所限制,所以未能下載全部的圖片,不過還是OK的

URL分析:

源碼:

#coding=utf-8import reimport urllibdef getHtml(url):    page=urllib.urlopen(url)    html=page.read();    return htmldef getImage(html):    reg=r‘src="(.*?\.jpg)" size‘    imgre=re.compile(reg)    imgeList =re.findall(imgre,html)    x=0    for image in imgeList:        urllib.urlretrieve(image,‘%s_hhh.jpg‘ % x)        x+=1html=getHtml("https://tieba.baidu.com/p/5256641773")getImage(html)

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.