-1

I'm trying to get the img alt portion of the element, but for some reason it will not register as an attribute of the element even though it is included in the printout of the string. How can I pull the img alt too (I need the text that says Boxed Warning but I'm not sure how to fix/make my code get it)?

page=requests.get('http://www.drugs.com/labeling-changes/May-2016.html')
noStarchSoup=bs4.BeautifulSoup(page.text, "html.parser")
elems=noStarchSoup.select('tr td > a')
print(elems[0].getText())
print(str(elems[1]))
print(elems[1].get('alt'))
print(elems[1].attrs)

Anaprox
<a href="/labeling-changes/May-2016/anaprox-naproxen-4321.html" rel="nofollow"><img alt="Boxed Warning" height="16" src="/img/icons/exclamation.png" title="Changes have been made to the Boxed    Warning section of the safety label." width="16"/></a>
None
{'href': '/labeling-changes/May-2016/anaprox-naproxen-4321.html', 'rel':['nofollow']}
  • Possible duplicate of [Extracting an attribute value with beautifulsoup](http://stackoverflow.com/questions/2612548/extracting-an-attribute-value-with-beautifulsoup) – Padraic Cunningham Jul 14 '16 at 19:51

1 Answers1

0

Printing the first element output this

print(elems[1])
  <a href="/labeling-changes/May-2016/anaprox-naproxen-4321.html" rel="nofollow">
    <img alt="Boxed Warning" height="16" src="/img/icons/exclamation.png" title="Chan
ges have been made to the Boxed Warning section of the safety label." width="16"/>
  </a>

So the first element a has a child named img. So you want to retrieve the alt attribute of that child.

print(elems[1].img.get('alt', ''))
Boxed Warning
print(elems[1].img.get('width', ''))
16
print(elems[1].img.get('height', ''))
16

But the width and the height doesn't mean that this value is the original size of the image.

Papouche Guinslyzinho
  • 5,277
  • 14
  • 58
  • 101