Сохранить все картинки png со страницы
Как сохранить все картинки, а именно .png, которые находятся на странице? Мой код не работает:
import requests
from bs4 import BeautifulSoup
response = requests.get("https://www.soccerstand.com/ru/match/4haX78Hq/#/match-summary")
soup = BeautifulSoup(response.content, "html.parser")
images = [img for img in soup.find_all("img") if ".png" in img.get("src")]
for i, image in enumerate(images):
img_data = requests.get(image["src"]).content
with open(f"image_{i}.png", "wb") as f:
f.write(img_data)
Ответы (1 шт):
Автор решения: Phoenix0757
→ Ссылка
попробуй так
import requests
from bs4 import BeautifulSoup
response = requests.get("https://www.soccerstand.com/ru/match/4haX78Hq/#/match-summary")
soup = BeautifulSoup(response.content, "html.parser")
images = [img for img in soup.find_all("img") if ".png" in img.get("src")]
for i, image in enumerate(images):
img_url = image["src"]
if not img_url.startswith("http"):
# Если указано относительное URL, необходимо дополнить его до абсолютного
img_url = "https://www.soccerstand.com" + img_url
img_data = requests.get(img_url).content
with open(f"image_{i}.png", "wb") as f:
f.write(img_data)
(должно сработать)