如何使用BeautifulSoup打印div的内部内容?

时间:2018-01-15 22:04:16

标签: python html python-3.x beautifulsoup

网站的html如下:

<div class="breed-image">
    <img src = "link to image">
</div>

当我这样做时:

soup = BeautifulSoup(response.text, 'lxml')
for link in soup.find_all(class_='breed-image'):

    print(link)

所有这一切都打印出来:

<div class="breed-image">
</div>

我也尝试过print(link.text)

所有这一切都打印出来:

None

感谢任何形式的帮助,谢谢!

2 个答案:

答案 0 :(得分:0)

几个选项:

>>> soup.img['src']
'link to image'
>>> for link in soup.find_all('img'):
...     print(link['src'])
...
link to image

答案 1 :(得分:0)

看起来您可能最好点击此页面调用的API来获取其图像:

In [13]: r = requests.get('https://dog.ceo/api/breeds/image/random')

In [14]: r.json()
Out[14]:
{'message': 'https://dog.ceo/api/img/terrier-dandie/n02096437_1790.jpg',
 'status': 'success'}