Issue
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?
Solution
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
Answered By - Mathyn
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.