2

I'm making a Python md5 decryptor from an API, but the problem is the API is sending back an HTML feedback. How do I get the text between the <font color=green> ?

{"error":0,"msg":"<font color=blue><b>Live</b></font><font color=green>Jumpman#23</font> | [MD5 Decrypt] .S/C0D3"}
1
  • 4
    Use an HTML parser such as bs4 (BeatifulSoup) Commented Apr 17, 2019 at 15:56

3 Answers 3

2

I suggest using an HTML parser as Beautiful Soup:

>>> from bs4 import BeautifulSoup
>>> d = {"error":0,"msg":"<font color=blue><b>Live</b></font><font color=green>Jumpman#23</font> | [MD5 Decrypt] .S/C0D3"}
>>> soup = BeautifulSoup(d['msg'], 'html.parser')
>>> soup.font.attrs
{'color': 'blue'}

You will get a dict that contains key, value pars as attribute name, value.

Update

To get the text "Jumpman#23"

>>> soup.findAll("font", {"color": "green"})[0].contents[0]
'Jumpman#23'
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer
@LimitedBrainCells, would you mind validating the answer please if this was useful?
0

If you know the target text will be exactly <font color=green>, then you can use simple string operations:

msg = "<font color=blue><b>Live</b></font><font color=green>Jumpman#23</font> | [MD5 Decrypt] .S/C0D3"
start_pattern = "<font color=green>"
stop_pattern = "<"
start_index = msg.find(start_pattern) + len(start_pattern)
stop_index = start_index + msg[start_index:].find(stop_pattern)
print msg[start_index:stop_index]

Comments

0

You could use bs4 and an adjacent sibling combinator for font tag

from bs4 import BeautifulSoup as bs
s = {"error":0,"msg":"<font color=blue><b>Live</b></font><font color=green>Jumpman#23</font> | [MD5 Decrypt] .S/C0D3"}
soup = bs(s['msg'], 'lxml')
data =  soup.select_one('font + font').text
print(data)

1 Comment

is the font colour important or does this need to apply more generally to between ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.