当前位置: 动力学知识库 > 问答 > 编程问答 >

python - How to extract <img> src from an <a> tag with a certain class?

问题描述:

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']

网友答案:

You are confusing yourself by sometimes calling .find() and sometimes .findAll(). You are further confusing yourself by sometimes using for x in y and sometimes x = y.

Use the for X in findAll() pattern exclusively:

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']
网友答案:
divdata = soup2.findAll('div', {"class": "preview"})

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

If you get the div tag, .img will get the next img in div tag's decendents.

分享给朋友:
您可能感兴趣的文章:
随机阅读: