如何使用Beautifulsoup获取img alt text和data-src?

时间:2016-10-27 11:56:51

标签: python web-scraping beautifulsoup

HTML片段:

<span class="photo_tooltip" id="__w2_YFXobXt_link">
<a href="/profile/Smit-Soni-2" id="__w2_GDetCwt_link">
<img class="profile_photo_img" src="https://assets.ec.quoracdn.net/-images.placeholder_img.png96cbdb37c749e493.png" height="50" width="50" data-src="https://assets.ec.quoracdn.net/main-thumb-18048885-50-ujrumofdevpkaarfisuvjdtbihztxnta.jpeg" alt="Smit Soni" />
</a></span>

我想从alt中提取所有data-src文字和img class="profile_photo_img。我的代码:

ele = soup.find_all('img', class_='profile_photo_img')
    for i in ele:
        print i["data-src"]
        print i["alt"]

但它什么都没打印。我怎样才能得到理想的结果?

1 个答案:

答案 0 :(得分:0)

请尝试以下代码:

elements = soup.findAll('img', attrs={'class':'profile_photo_img'})
for element in elements:
    print element['data-src']
    print element['alt']

还要确保在搜索元素之前通过打印来正确解析html中的汤