一個鹹魚的Python爬蟲之路(三):爬取網頁圖片,

來源:互聯網
上載者:User

一個鹹魚的Python爬蟲之路(三):爬取網頁圖片,

學完Requests庫與Beautifulsoup庫我們今天來實戰一波,爬取網頁圖片。依照現在所學只能爬取圖片在html頁面的而不能爬取由JavaScript產生的圖。
所以我找了這個網站http://www.ivsky.com

 

網站裡面有很多的圖集,我們就找你的名字這個圖集來爬取

 

   http://www.ivsky.com/bizhi/yourname_v39947/來看看這個頁面的原始碼:

可以看到我們想抓取的圖片資訊在<ul>中而網頁中不止一個ul所以要將後面class一起抓取 裡面然後圖片地址在img裡面那麼我們這裡可以用BeautifulSoup庫方法來解析網頁並抓取圖片資訊。

soup =BeautifulSoup(html,'html.parser')
all_img= (soup.find('ul', class_='pli').find_all('img'))
for img in all_img:
src=img['src']


url方面我們用requests庫去擷取:

def getHtmlurl(url):         #擷取網址    try:       r=requests.get(url)       r.raise_for_status()       r.encoding=r.apparent_encoding       return r.text    except:        return ""

我們要將圖片下載下來並存在本地:

       try:                              #建立或判斷路徑圖片是否存在並下載           if not os.path.exists(root):               os.mkdir(root)           if not os.path.exists(path):               r = requests.get(img_url)               with open(path, 'wb') as f:                   f.write(r.content)                   f.close()                   print("檔案儲存成功")           else:               print("檔案已存在")       except:           print("爬取失敗")


整個爬蟲的架構與思路:

import requestsfrom bs4 import BeautifulSoupimport osdef getHtmlurl(url):  #擷取網址passdef getpic(html): #擷取圖片地址並下載passdef main(): 主函數pass


這裡給出完整代碼

import requestsfrom bs4 import BeautifulSoupimport osdef getHtmlurl(url):         #擷取網址    try:       r=requests.get(url)       r.raise_for_status()       r.encoding=r.apparent_encoding       return r.text    except:        return ""def getpic(html): #擷取圖片地址並下載    soup =BeautifulSoup(html,'html.parser')    all_img=soup.find('ul',class_='pli')find_all('img')    for img in all_img:       src=img['src']       img_url=src       print (img_url)       root='D:/pic/'       path = root + img_url.split('/')[-1]       try:                              #建立或判斷路徑圖片是否存在並下載           if not os.path.exists(root):               os.mkdir(root)           if not os.path.exists(path):               r = requests.get(img_url)               with open(path, 'wb') as f:                   f.write(r.content)                   f.close()                   print("檔案儲存成功")           else:               print("檔案已存在")       except:           print("爬取失敗")def main():    url='http://www.ivsky.com/bizhi/yourname_v39947/'    html=(getHtmlurl(url))    print(getpic(html))main()


運行代碼:

 我們可以看到圖片都儲存在本地了這就是簡單的實戰案列,大家可以自己試試。

 

聯繫我們

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