0

I am trying to look through a very long string in Python and I am looking for the value that is in

<p class="price">$VALUE USD</p>

I am currently doing

look = "<p class=\"price\">";
print(site.text.find(look));

Because the will always be there but I am looking for the value of the rest of the line. The value is always changing too so it needs to be flexible. At the end I hope to be able to save something like

<p class="price">$2345.22</p>

to a string.

qwessin
  • 11
  • 1
  • 1
    related: https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Tadhg McDonald-Jensen Nov 22 '21 at 18:10
  • 1
    The text you are looking for is HTML. If your "very long string" is all HTML, you should use [BeautifulSoup](https://www.crummy.com/software/BeautifulSoup/bs4/doc/) – Wondercricket Nov 22 '21 at 18:11

2 Answers2

0
import re

text = "<p class=\"price\">$2345.22</p>"
regex = re.compile("<p class=\"price\">\\$([.\\d]*)</p>")
print(regex.match(text).group(1)) # -> 2345.22
Hell stormer
  • 413
  • 1
  • 4
  • 14
0

You can use BeautifulSoup to easily get the text from within the tags. https://www.crummy.com/software/BeautifulSoup/bs4/doc/#get-text

Another way to get the value will be to use the python split method consecutively

string = '<p class="price">$VALUE USD</p>'
new_string = string.split(">")[1].split("<")[0]
Elisha Veve
  • 81
  • 1
  • 3