Python Forum
Using beautiful soup to get html attribute value
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
Reply
#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'
Reply
#3
Thank you very much Snippsat :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Beautiful Soup - access a rating value in a class KatMac 1 3,420 Apr-16-2021, 01:27 PM
Last Post: snippsat
  HTML multi select HTML listbox with Flask/Python rfeyer 0 4,530 Mar-14-2021, 12:23 PM
Last Post: rfeyer
  *Beginner* web scraping/Beautiful Soup help 7ken8 2 2,561 Jan-28-2021, 04:26 PM
Last Post: 7ken8
  Help: Beautiful Soup - Parsing HTML table ironfelix717 2 2,622 Oct-01-2020, 02:19 PM
Last Post: snippsat
  Beautiful Soup (suddenly) doesn't get full webpage html j.crater 8 16,386 Jul-11-2020, 04:31 PM
Last Post: j.crater
  Requests-HTML vs Beautiful Soup - How to Choose? robin73 0 3,780 Jun-23-2020, 02:53 PM
Last Post: robin73
  html data cell attribute issue delahug 5 3,085 May-31-2020, 09:18 AM
Last Post: delahug
  Python3 + BeautifulSoup4 + lxml (HTML -> CSV) - How to loop to next HTML/new CSV Row BrandonKastning 0 2,329 Mar-22-2020, 06:10 AM
Last Post: BrandonKastning
  looking for direction - scrappy, crawler, beautiful soup Sly_Corn 2 2,402 Mar-17-2020, 03:17 PM
Last Post: Sly_Corn
  Beautiful soup truncates results jonesjoz 4 3,798 Mar-09-2020, 06:04 PM
Last Post: jonesjoz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020