python bs4 extract from attribute inside button class

Zero Sense

So im trying to get the value of a attribute using BeautifulSoup4.

replay_url_data = matchdatatr[1].findAll("button",{"class":"replay_button_super"})

This is how i get all my data into the object. Typing the replay_url_data into the console returns :

<button class="replay_button_super" data-client-version="0.0.2.21" data-rel="spectatePopup" data-spectate-encryptionkey="bPPxpLIDmi0hRfU2U8B9Li1VJfTTx6pZ" data-spectate-endpoint="replays.cosmicradiance.com:80" data-spectate-gameid="4339075348" data-spectate-link="/api/spectate/UEJPNkN4MkIwUkZERUJ0MWUyZ3dDTmxGT25kanlUN2V6YnpuZUQ0bVlyMWRReGNDRXprZ1lQVnRnSkNHMG04Y2hUdVhxQm9abHFsQ2VBaTRaYVFPdnc9PQ==" data-spectate-platform="Modigu1" data-width="640"><i class="fa fa-play"></i>Replay</button>

What i want is to get the value of data-spectate-link.

I have tried every google result i found about similar topics but nothing worked.

replay_url_split = replay_url_data[0].findAll("button",{"class":"data-spectate-link"})

This returns "[]" empty.

replay_url_data[0].find('data-spectate-platform')

This returns same result empty

replay_url_data[0].find('button',attrs={'class' : 'data-spectate-link'})

And this one returns the same as above "[]" empty.

After 3 hours of searching on google so far nothing has helped me and im getting desperate.Im still new to python and html so excuse my stupidity.

furas

To get attribute you use .attrs["data-spectate-link"] or directly ["data-spectate-link"]


Example

from bs4 import BeautifulSoup as BS

text = '<button class="replay_button_super" data-client-version="0.0.2.21" data-rel="spectatePopup" data-spectate-encryptionkey="bPPxpLIDmi0hRfU2U8B9Li1VJfTTx6pZ" data-spectate-endpoint="replays.cosmicradiance.com:80" data-spectate-gameid="4339075348" data-spectate-link="/api/spectate/UEJPNkN4MkIwUkZERUJ0MWUyZ3dDTmxGT25kanlUN2V6YnpuZUQ0bVlyMWRReGNDRXprZ1lQVnRnSkNHMG04Y2hUdVhxQm9abHFsQ2VBaTRaYVFPdnc9PQ==" data-spectate-platform="Modigu1" data-width="640"><i class="fa fa-play"></i>Replay</button>'
soup = BS(text, 'html.parser')

all_buttons = soup.findAll("button", {"class": "replay_button_super"})
one_button = all_buttons[0]

value = one_button["data-spectate-link"]
print(value)

value = one_button.attrs["data-spectate-link"]
print(value)

BTW: If you want to search buttons with attribute data-spectate-link then you have to search

{"data-spectate-link": True}

not {"class": "data-spectate-link"}


Example

from bs4 import BeautifulSoup as BS

text = '''<button>Other button</button>
<button>Other button</button>
<button>Other button</button>
<button class="replay_button_super" data-client-version="0.0.2.21" data-rel="spectatePopup" data-spectate-encryptionkey="bPPxpLIDmi0hRfU2U8B9Li1VJfTTx6pZ" data-spectate-endpoint="replays.cosmicradiance.com:80" data-spectate-gameid="4339075348" data-spectate-link="/api/spectate/UEJPNkN4MkIwUkZERUJ0MWUyZ3dDTmxGT25kanlUN2V6YnpuZUQ0bVlyMWRReGNDRXprZ1lQVnRnSkNHMG04Y2hUdVhxQm9abHFsQ2VBaTRaYVFPdnc9PQ==" data-spectate-platform="Modigu1" data-width="640"><i class="fa fa-play"></i>Replay</button>
<button>Other button</button>
<button>Other button</button>'''

soup = BS(text, 'html.parser')

all_buttons = soup.findAll("button", {"data-spectate-link": True})
one_button = all_buttons[0]

value = one_button["data-spectate-link"]
print(value)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Get values from onclick attribute using python bs4

Extract from webpage using bs4 and Python

Python 3 BS4 - Extract Data from <span> tags

How can i scrape url from tag without class or id inside another tag in bs4 [ python 3 ]

Python - BS4 - Extracting multiple values from a class

Class attribute is not included in bs4 object

Select a tag inside a class with bs4

scrape a sub attribute? with bs4 in python

How to get attribute value from li tag in python BS4

Web Scraping, How to extract data from two same tags using bs4 in python

How should I properly extract and parse subject data from a webpage using python and BS4?

Get only the name from a dd tag on a website using BS4 and Python, not the entirety of the dd class

Restrict class from overriding parent attribute inside class definition in python

Getting data inside of button. BS4 + Requests

Find all tags of multi-attribute class bs4

Python class attribute inside of class attribute

BS4 text inside <span> which has no class

How to extract attribute inside class (?) with beautifulsoup?

How can I find my desired table inside a set of nested tables, using bs4/python, if there are no class or id attributes?

How to scrape id attribute from HTML with bs4?

Extract specific value from HTML with bs4

How to extract text from outside of tag with BS4

Extract a content from <script> scrapign with BS4

bs4 extract text from multiple spans in a tag

Python bs4 'bytes' object has no attribute 'prettify'

Python BS4 Unsupported format: White space in attribute selector

BS4: Attribute Error in Web Scraping with Python

Python bs4: extract/select parts of html divs

Extract an unusual XML tag with BS4 and Python