Python beautifulsoup:从img s获取占位符

2024-10-06 00:30:16 发布

您现在位置:Python中文网/ 问答频道 /正文

在BeautifulSoup的帮助下,我试着从主页上读取图像的图像地址。 在页面源文本中,我看到了图像的URL。 但是如果我尝试用BeautifulSoup的命令find_all读取地址,我只会得到一个图像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=""/>]

我希望任何人都能给我一个提示或为什么我得到一个占位符,而不是原来的图像网址。你知道吗

我的代码:

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))

Tags: 图像srcurlimg地址issueallfind