使用BeautifulSoup从网页的特定部分抓取所有图像

时间:2020-08-21 14:30:05

标签: python web-scraping beautifulsoup

对象'画廊'是我所得到的-我将如何仅选择图像URL而无需花费很多时间。

当前,我正在执行以下操作

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是可迭代库的长度。

也许是循环? :s

感谢CN

1 个答案:

答案 0 :(得分:1)

您可以使用嵌套循环加载所有图像并将其存储到列表中。例如:

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
# ...