我试图循环浏览所有的 img 类,但我不知道如何才能获得。src 联系
import requests
from bs4 import BeautifulSoup
url = 'https://giphy.com/search/anxiety'
html = requests.get(url).text
soup = BeautifulSoup(html, "html.parser")
gifs = soup.findAll("img", attrs={"class": "giphy-gif-img"})
for gif in gifs:
print(gif.get('image-src'))
在你的最后一行,你可以使用 gif.get('src')
.然而, gifs
为空,因为没有带有 class=giphy-gif-img
在页面上。