Extract IP address from an html string (python)
Posted
by GoJian
on Stack Overflow
See other posts from Stack Overflow
or by GoJian
Published on 2010-05-23T06:57:25Z
Indexed on
2010/05/23
7:00 UTC
Read the original article
Hit count: 202
My Friends,
I really want to extract a simple IP address from a string (actually an one-line html) using Python. But it turns out that 2 hours passed I still couldn't come up with a good solution.
>>> s = "<html><head><title>Current IP Check</title></head><body>Current IP Address: 165.91.15.131</body></html>"
-- '165.91.15.131' is what I want!
I tried using regular expression, but so far I can only get to the first number.
>>> import re
>>> ip = re.findall( r'([0-9]+)(?:\.[0-9]+){3}', s )
>>> ip
['165']
In fact, I don't feel I have a firm grasp on reg-expression and the above code was found and modified from elsewhere on the web.
Seek your input and ideas!
© Stack Overflow or respective owner