首页 > 解决方案 > Python beautifulsoup:从 img src 获取占位符

问题描述

在 BeautifulSoup 的帮助下,我尝试从主页读取图像的图像地址。在页面源文本中,我看到了图像的 URL。但是,如果我尝试使用find_allBeautifulSoup 中的命令读取地址,我只会得到图像 URL 的占位符。

图片中的 URL 结构如下:

<a href="Young-Justice-(2019)/Young-Justice-(2019)-Issue-11/3"><br /><img src="mangas/Young Justice (2019)/Young Justice (2019) Issue 11/cw002.jpg" alt="" width="1200" height="1846" class="picture" /></a>

在 BeautifulSoup 我得到这个:

<img 0="" alt="" class="picture" height="" src="/pics/placeholder2.jpg" width=""/>]

我希望任何人都可以给我一个提示或为什么我得到一个占位符而不是原始图像 url。

我的代码:

import requests
from bs4 import BeautifulSoup as BS
from requests.exceptions import ConnectionError


def getimageurl(url):
    try:
        response = requests.get(url)
        soup = BS(response.text, 'html.parser')
        data = soup.find_all('a', href=True)

        for a in data:
            t = a.find_all('img', attrs={'class': 'picture'})
            print(t)
    except ConnectionError:
        print('Cant open url: {0}'.format(url))

标签: pythonimagebeautifulsoupplaceholdersrc

解决方案


推荐阅读