如何从 html 抓取图片,图片不在 img 标签中,而是在 div class='blah blah' 下

how can I grab an image from html, the image is not in img tag but under div class='blah blah'

我今天学习了如何在 Python 上使用 BeautifulSoup.select('blahblah') 从互联网上抓取图片以及如何在我的 mac.

上下载它们

我可以下载 img 标签中的照片,并像这样找到它们: src='http or // blah blah'.

但我在 'div' class='something else' 下找不到任何 .jpg.png 部分。

我想下载的图片不仅仅是一张图片,而且似乎还有其他功能,例如显示登录弹出窗口,上面有一个按钮可以使图片变大。

import lxml
import bs4
import requests

rec = requests.get('https://www.pinterest.com/pin/701294973197421148/')
soup_rec = bs4.BeautifulSoup(rec.text, 'lxml')

soup_rec

soup_rec.select('div.zI7.iyn.Hsu') # I just type this way to try anything.

我尝试下载的图片是 Pinterest 页面中间的一位女演员的照片。

您需要 selenium 为此目的,因为这是一个动态网站:

import bs4 as Bs
from selenium import webdriver

DRIVER_PATH = 'path/to/your/executable'
driver = webdriver.Chrome(executable_path=DRIVER_PATH)

driver.get('https://www.pinterest.com/pin/701294973197421148/')

page_src = Bs.BeautifulSoup(driver.page_source)

img = page_src.find("div",{"class":"zI7 iyn Hsu"}).find("img")

print(img.get_attribute_list("src")[0])

给我:

'https://i.pinimg.com/originals/ac/8b/d7/ac8bd7a2ff22210e8592a2aae202490d.jpg'

您可以使用 this guide 了解如何使用 selenium 抓取动态网站。