Kali ini kita coba gunakan Python untuk mengunduh gambar dengan resolusi tertinggi dari laman web (web page) target.
Berikut adalah gambar yang berhasil di-download dari URL target.
import requests
from os.path import basename
from bs4 import BeautifulSoup
from PIL import Image
import io
import matplotlib.pyplot as plt
import numpy as np
#URL target (disini ada beberapa gambar yang bisa di-download)
url = 'http://www.teknologi-bigdata.com/2019/08/buku-teknologi-big-data-edisi-revisi.html'
r = requests.get(url)
soup = BeautifulSoup(r.content)
max_res = 0
for link in soup.select('img[src^=http]'):
lnk = link['src']
img_bytes = requests.get(lnk).content
img_file = Image.open(io.BytesIO(img_bytes))
width, height = img_file.size
resolution = width * height
if max_res < resolution:
max_res = resolution
max_size = str(width) + 'x' + str(height)
max_img_file = img_file
print('highest res = ' + str(max_res) + ', max size = ' + max_size)
max_img_file.save('featured_image.' + str(max_img_file.format))
print('featured_image saved')
#Tampilkan gambar yang sudah di-download dengan MatplotLib
plt.imshow(np.array(max_img_file))
highest res = 327680, max size = 512x640
featured_image saved
Berikut adalah gambar yang berhasil di-download dari URL target.