Skip to content
Advertisement

Finding a span tag with a ‘variable’? but no class – Beautiful soup/Python

I am using BeautifulSoup and Python to find a span tag that doesnt seem to have a class.

I am wanting to get the text “1hr ago” in the span tag, it has a… Variable? called “data-automation” but I can’t seem to find out how to find that using beautiful soup.

The first span has a class of “_3mgsa7- _2CsjSEq _2gpxOIH _15GBVuT _3VdCwhL _2Ryjovs” which does produce the text using my code but it also has an error.

can anyone help me fix the error or explain how I would find the “data-automation” span tag?

MY CODE:

JavaScript

ERROR:

JavaScript

WEBSITE HTML CODE:

JavaScript

Advertisement

Answer

You can select a <span> element with a specific attribute (such as data-automation) by passing an attrs dict as a keyword argument to .find() or .find_all(). See the documentation.

To find <span>‘s where data-automation has any value:

JavaScript

Where data-automation has a specific value:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement