Record global IP with python

A little explanation

code

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

Record global IP with python
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Record with Python → Save file (sounddevice + wave)
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Overthrow, Python Global Declaration
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
Run prepDE.py with python3
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
Posting tweets with python
Drive WebDriver with python
Use mecab with Python3
[Python] Redirect with CGIHTTPServer
Voice analysis with python
Think yaml with python
Operate Kinesis with Python
Getting Started with Python
Use DynamoDB with Python
Zundko getter with python
Handle Excel with python
Ohm's Law with Python
Primality test with python
Run Blender with python
Solve Sudoku with Python