Reputation:
Im creating a program that prints out the Ip adress of the User. So what im trying to do is to get the Html of ipchicken.com and print out only the "Name Address" Part. here is my code so far:
import urllib
sock = urllib.urlopen("http://ipchicken.com")
htmlSource = sock.read()
sock.close()
print htmlSource
Now how do i get the ip part of the html printed out?
and if there are other ways to get an ip of the user using python,please include that aswell :)
Upvotes: 1
Views: 1439
Reputation: 16525
Just run a regex to find IP structure patterns over htmlSource
ips = re.findall('(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})',
htmlSource)
the variable ips
will contain all literals with a IP structure.
The whole code would look like:
import urllib,re
sock = urllib.urlopen("http://ipchicken.com")
htmlSource = sock.read()
sock.close()
print htmlSource
ips = re.findall('(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})', htmlSource)
print "IPs in page", ips
Upvotes: 2
Reputation: 72745
I would suggest that you use something that's more programmatic like ifconfig.me rather than ipchicken which is ad laden. ifconfig.me behaves different when being queried by something like cURL.
If you want to parse the HTML and get out the IP address with ipchicken, use BeautifulSoup or ElementTree.
update : http://ip.appspot.com/ is something that has only a programmatic interface.
Upvotes: 2
Reputation: 17606
You can use a regular expression to grab a text within another text. If you provide the relevant part of your htmlSource
we can post one.
Here are some posts on retrieving the IP address: How to find out your IP address in Python
Upvotes: 1