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
  HTML Styling Not Working yoitspython 1 109 Aug-13-2019, 06:26 AM
Last Post: fishhook
  spliting html code with br tag yokaso 11 265 Aug-07-2019, 03:18 PM
Last Post: snippsat
  How do I get rid of the HTML tags in my output? glittergirl 1 312 Aug-05-2019, 08:30 PM
Last Post: snippsat
  convert html table to json bhojendra 5 159 Jul-30-2019, 07:53 PM
Last Post: DeaD_EyE
  How to capture Single Column from Web Html Table? ahmedwaqas92 5 287 Jul-29-2019, 02:17 AM
Last Post: ahmedwaqas92
  Getting a specific text inside an html with soup mathieugrimbert 9 367 Jul-10-2019, 12:40 PM
Last Post: mathieugrimbert
  Beautiful soup and tags starter_student 11 525 Jul-08-2019, 03:41 PM
Last Post: starter_student
  getting options from a html form pgoosen 5 344 Jul-03-2019, 06:07 PM
Last Post: nilamo
  Beautiful Soup find_all() kirito85 2 287 Jun-14-2019, 02:17 AM
Last Post: kirito85
  [Flask] html error 405 SheeppOSU 0 183 Jun-08-2019, 04:42 PM
Last Post: SheeppOSU

Forum Jump:


Users browsing this thread: 1 Guest(s)