The following is an example of getting it at here.
ip.py
#!/usr/bin/env python
import requests
from HTMLParser import HTMLParser
import codecs
class MYHTMLParser(HTMLParser): #Overriding the HTMLParser class
def __init__(self): #Initialization
HTMLParser.__init__(self)
self.mytag = ''
def handle_starttag(self,tag, attrs): #Handler called read at the beginning of the tag
if tag == 'p': #Tag identification
if (dict(attrs).get('name')=="ip"): #Attribute determination
self.mytag = 'ip'
def handle_data(self,data): #Handler called read data
if self.mytag == 'ip': #Attribute determination
self.mytag = '' #Initialize the flag here
print 'IP='+data
with codecs.open('my_ip.txt','w','utf-8') as f:
f.write(data)
def ip_get(myurl): #Main processing
r = requests.get(myurl)
r.encoding = r.apparent_encoding #Encoded because it could not be processed as it is
with codecs.open('my_ip.html','w','utf-8') as f:
f.write(r.text)
f.flush() #Just in case, I added it.
with codecs.open('my_ip.html','r','utf-8') as f:
parser = MYHTMLParser()
parser.feed(f.read())
parser.close()
if __name__ == '__main__':
ip_get("http://www.axisnetworks.biz/tools/gip/")
The retrieved page is saved in `my_ip.html
, and the IP is saved in `my_ip.txt
.
The encoding process is a painful result. .. ..
Recommended Posts