0

Как сохранить все картинки, а именно .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
  • soup.find_all("img") не находить тег "img". Уж постарайтесь сделать соответствующие выводы. Проанализируйте response.text на наличия нужных вам данных. 5 фев в 9:11

1 ответ 1

0

попробуй так

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)

(должно сработать)

1
  • а проверить забыли)) 5 фев в 12:11

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.