[英]Scraping all the images from a specific part of a webpage using BeautifulSoup
Object '圖庫' 是我得到的 - 我怎么能只是 select 圖像網址而不走很長的路。
目前,我正在執行以下操作
from bs4 import BeautifulSoup
from PIL import Image
import requests
gallery = soup.findAll(class_='gallery')
img_0 = gallery[0].find('img')
img_1 = gallery[1].find('img')
...
img_x = gallery[x].find('img')
img_url_0 = img_0['src']
img_url_1 = img_1['src']
...
img_url_x = img_x['src']
gallery_img_0 = Image.open(requests.get(img_url_0, stream = True).raw)
gallery_img_1 = Image.open(requests.get(img_url_1, stream = True).raw)
...
gallery_img_x = Image.open(requests.get(img_url_x, stream = True).raw)
其中 x 是可迭代庫的長度。
也許是一個循環? :秒
謝謝,CN
您可以使用嵌套循環加載所有圖像並將它們存儲到列表中。 例如:
galleries = soup.findAll(class_='gallery')
all_images = []
for gallery in galleries:
for img in gallery.findAll('img'):
gallery_img = Image.open(requests.get(img['src'], stream = True).raw)
all_images.append(gallery_img)
# here, `all_images` contains all images
# ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.