如何从<a> tag with a certain class?

时间:2017-01-26 21:39:29

标签: python beautifulsoup

I have been trying to get this to work for a while but can't figure it out. There is an <a> tag inside a <div>. Inside the <a> tag is an <img> This is what the html looks like:

<div class="preview">
    <a class="zoom" href="#"><img src="theimage.jpg" alt="drink"></a>
</div>

My BeautifulSoup code:

divdata = soup2.findAll('div', {"class": "preview"})
for getatag in divdata.find('a', {'class': 'zoom'}):
    for getimgtag in getatag.findAll('img',src=True):
        print getimgtag['src']

2 个答案:

答案 0 :(得分:1)

您有时会打电话给.find(),有时打电话给.findAll(),这让您感到困惑。您有时会使用for x in y,有时使用x = y来进一步混淆自己。

仅使用for X in findAll()模式:

for divdata in soup2.findAll('div', {"class": "preview"}):
    for getatag in divdata.findAll('a', {'class': 'zoom'}):
        for getimgtag in getatag.findAll('img',src=True):
            print getimgtag['src']

答案 1 :(得分:0)

divdata = soup2.findAll('div', {"class": "preview"})

for div in divdata:
    print(div.img.get('src'))

如果您获得了div代码,则.img将获得img代码后代中的下一个div