Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Using beautiful soup to get html attribute value
#1
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
Quote
#2
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'
Quote
#3
Thank you very much Snippsat :)
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Getting a specific text inside an html with soup mathieugrimbert 9 1,502 Jul-10-2019, 12:40 PM
Last Post: mathieugrimbert
  Beautiful soup and tags starter_student 11 786 Jul-08-2019, 03:41 PM
Last Post: starter_student
  Beautiful Soup find_all() kirito85 2 485 Jun-14-2019, 02:17 AM
Last Post: kirito85
  [split] Using beautiful soup to get html attribute value moski 6 830 Jun-03-2019, 04:24 PM
Last Post: moski
  Failure in web scraping by Beautiful Soup yeungcase 4 1,061 Mar-23-2019, 12:36 PM
Last Post: metulburr
  Beautiful soup won't find value even with CSS path copied. AdequatelyChilled 4 805 Jan-01-2019, 12:12 PM
Last Post: snippsat
  Need help with Beautiful Soup - table jlkmb 9 983 Dec-20-2018, 01:10 AM
Last Post: jlkmb
  using regex wildcard Beautiful Soup Larz60+ 6 2,673 Sep-27-2018, 09:19 PM
Last Post: Larz60+
  Beautiful Soup - Title + Paragraph into a text file dj99 4 1,957 Jul-14-2018, 01:37 PM
Last Post: dj99
  Beautiful Soup - Delete All HTML - Except Specific Classes dj99 7 1,844 Jul-13-2018, 08:18 AM
Last Post: dj99

Forum Jump:


Users browsing this thread: 1 Guest(s)