开发者

How to Get a line of text from html to Python

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 开发者_如何学Cof the user using python,please include that aswell :)


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


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.


Use a HTML scraping library, like BeautifulSoup.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜