python網絡爬蟲之爬取圖片


今天使用requests和BeautifulSoup爬取了一些圖片,還是很有成就感的,注釋可能有誤,希望大家多提意見:

方法一:requests

import requests
from bs4 import BeautifulSoup
circle = requests.get('http://travel.quanjing.com/tag/12975/%E9%A9%AC%E5%B0%94%E4%BB%A3%E5%A4%AB')

# 將獲取的圖片地址依次放入count中
count = []
# 將獲取的網頁內容放入BeautifulSoup
soup = BeautifulSoup(circle.text, 'lxml')
# 根據谷歌SelectGadGet這個插件,獲取html標簽,比如獲取:#gallery-list
for item in soup.select('#gallery-list'):
    # 用bs4中的find_all獲取 #gallery-list 中是否存在 img這個標簽
    for img in item.find_all('img'):
        print('img', img)
        # m 是 img標簽中存在的屬性
        img_path = img.get('m')
        count.append(img_path)
# 用enumerate依次取出count中的圖片地址 放入v中
for i,v in enumerate(count):
    # 將獲取的v值再次放入request中進行與網站相應
    image = requests.get(v)
    # 存取圖片過程中,出現不能存儲 int 類型,故而,我們對他進行類型轉換 str()。w:讀寫方式打開,b:二進制進行讀寫。圖片一般用到的都是二進制。
    with open('D:\\img'+str(i)+'.jpg', 'wb') as file:
        # content:圖片轉換成二進制,進行保存。
        file.write(image.content)
    print(i)

 方法二:urllib.request

from bs4 import BeautifulSoup
import urllib.request as ure
import os
url = 'http://travel.quanjing.com/tag/12975/%E9%A9%AC%E5%B0%94%E4%BB%A3%E5%A4%AB'
response = ure.urlopen(url, timeout=30)
circle = response.read().decode('utf-8')

# 將獲取的圖片地址依次放入count中
count = []
# 將獲取的網頁內容放入BeautifulSoup
soup = BeautifulSoup(circle, 'lxml')
# 根據谷歌SelectGadGet這個插件,獲取html標簽,比如獲取:#gallery-list
for item in soup.select('#gallery-list'):
    # 用bs4中的find_all獲取 #gallery-list 中是否存在 img這個標簽,limit:限制爬取的數量。
    for img in item.find_all('img', limit=5):
        print('img', img)
        # m 是 img標簽中存在的屬性
        img_path = img.get('m')
        count.append(img_path)
if not os.path.exists('photo'):
    os.makedirs('photo')
# 用enumerate依次取出count中的圖片地址 放入v中
for i,v in enumerate(count):
    # 存取圖片過程中,出現不能存儲 int 類型,故而,我們對他進行類型轉換 str()。w:讀寫方式打開,b:二進制進行讀寫。圖片一般用到的都是二進制。
    path = 'photo\\img'+str(i)+'.jpg'
    with open(path, 'w') as file:
        ure.urlretrieve(v, path)
    print(i)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM