0
<div class="media-body text-right">

  <h4 style="color:#fff;font-weight:bold"> 790.00</h4>
  <span style="color:white;font-weight:bold">Current Balance (Earn)</span>

 </div>

I want to get the value "790.00" from this html code and put it in a variable. How can i do this with python? please HELP.

Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • `x = '790.00 Current Balance (Earn)' value = x.split(' ')[0]` – balderman Aug 20 '21 at 20:26
  • Does this answer your question? [Parsing HTML using Python](https://stackoverflow.com/questions/11709079/parsing-html-using-python) – Piero Aug 20 '21 at 20:32
  • What did you try after `from bs4 import BeautifulSoup` already, any [example] to show? A simple [`soup.get_text()`](https://realpython.com/python-web-scraping-practical-introduction/#use-a-beautifulsoup-object) serves as start ️ – hc_dev Aug 20 '21 at 20:35
  • If you want to select only this ```
    ``` from the entire HTML, you need to either share the URL or the complete HTML code.
    – Ram Aug 21 '21 at 07:03

1 Answers1

0

Try:

from bs4 import BeautifulSoup

html_doc = """
<div class="media-body text-right">

  <h4 style="color:#fff;font-weight:bold"> 790.00</h4>
  <span style="color:white;font-weight:bold">Current Balance (Earn)</span>

 </div>
"""

soup = BeautifulSoup(html_doc, "html.parser")
val = soup.find("h4").get_text(strip=True)
print(val)

Prints:

790.00
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • Sir, how can i extract the particular part of the html code from the whole html source code. Actually, i need the value "790" only. –  Aug 20 '21 at 21:35
  • @AbidChowdhury Can you share the URL? To convert string `790.00` to `790` you can do `val = val.split(".")[0]` – Andrej Kesely Aug 20 '21 at 21:38
  • Sir , you didn't get me actually. I want to extract the following code from the whole webpage.

    790.00

    Current Balance (Earn)
    –  Aug 20 '21 at 21:48