Python Forum
Using beautiful soup to get html attribute value - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: Using beautiful soup to get html attribute value (/thread-17781.html)



Using beautiful soup to get html attribute value - graham23s - Apr-23-2019

Hi Guys,

What i'm trying to do is use beautiful soup to get the value of an html attribute.

<div class="g-recaptcha" data-sitekey="VALUE_TO_RETURN"></div>

What i have so far is:

soup = BeautifulSoup(html, "html.parser")
print("data-sitekey=" + soup.find("div", {"class" : "data-sitekey"}))
return soup.find("div", {"class" : "data-sitekey"})
But this returns "must be str, not nulltype"

any help would be appreciated.

cheers guys


RE: Using beautiful soup to get html attribute value - snippsat - Apr-23-2019

from bs4 import BeautifulSoup

html = '''\
<div class="g-recaptcha" data-sitekey="VALUE_TO_RETURN"></div>'''

soup = BeautifulSoup(html, 'lxml')
>>> tag = soup.find('div', class_="g-recaptcha")
>>> tag
<div class="g-recaptcha" data-sitekey="VALUE_TO_RETURN"></div>

>>> tag.attrs
{'class': ['g-recaptcha'], 'data-sitekey': 'VALUE_TO_RETURN'}
>>> tag.attrs.get('data-sitekey')
'VALUE_TO_RETURN'



RE: Using beautiful soup to get html attribute value - graham23s - Apr-23-2019

Thank you very much Snippsat :)