Using Python with Beautifulsoup to find span class data-automation-id value

Question:

Could someone please tell me how to use beautifulsoup to extract the value ($100.00) from a data-automation-id="header-account-balance"? I can see how to find elements by class (How to find elements by class) and id (Beautiful Soup and extracting a div and its contents by ID) but don’t know how to refine it for a "data-automation-id" value. I have scraped the following code:

<div class="contentContainer_ff3y6tu">
<div class="container_fikviug">
<div class="accountPillForPersonal_fituo0a">
<div class="balanceTextContainer_fu48fjw">
<div class="container_fdoc1b1">
<span class="size14_f7opyze" data-automation-id="header-account-balance">$100.00</span>
</div></div>
<div class="tokenCount_f19lbt28">
<span class="size12_fq5j3k2 CodGray_f1tc10rl bold_f1au7gae">7</span>
</div></div></div></div>

Any help would be greatly appreciated thanks.

Asked By: Kankan2000

||

Answers:

Given the example, you can do it this way:

soup = BeautifulSoup("<your html here>")
tag = soup.find("span", {"data-automation-id": "header-account-balance"})
value = tag.string
Answered By: Dmytro Hrimov
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.