2

HTML snippet :

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

I want to extract all the alt text and data-src from the img class="profile_photo_img. My code:

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

But it is printing nothing. How can I get the desired result?

3
  • 2
    your code works fine Commented Oct 27, 2016 at 12:06
  • @e4c5 No, it is printing nothing Commented Oct 27, 2016 at 12:09
  • @slpcf: Please also share how you are creating the object soup Commented Oct 27, 2016 at 12:21

1 Answer 1

3

Try the following code:

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

Also make sure that the soup is right parsed from the html by printing it before you are searching for elements

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.