상세 컨텐츠

본문 제목

Module - path, urllib, json, html, xml

Python

by techbard 2015. 8. 3. 21:50

본문

반응형
  • path module

# path module

 

import os
import shutil
from os import path


print(os.name)
print(path.exists('c:\windows'))
print(path.isdir('c:\windows'))
print(path.isfile('c:\windows\explorer.exe'))

if path.exists('c:\windows\explorer.exe'):
    src = path.realpath('c:\windows\explorer.exe')

    print(src)

    head, tail = path.split(src)
    print('path:', head)
    print('file:', tail)

 

결과)

 

nt
True
True
True
c:\windows\explorer.exe
path: c:\windows
file: explorer.exe

  • urllib module

# HTTP 에러코드 얻기

 

import urllib.request

req = urllib.request.Request('http://www.pretend_server.org')
try:
    urllib.request.urlopen(req)
except urllib.error.URLError as e:
    print(e.reason)

 

결과)

[Errno 11004] getaddrinfo failed

  • json module

# json 데이터 읽기


import urllib.request

import urllib

import json


def printResule(data):

theJSON = json.loads(data.decode())


if 'title' in theJSON['metadata']:

print(theJSON['metadata']['title'])


if 'count' in theJSON['metadata']:

count = theJSON['metadata']['count']

print(str(count), 'events recorded.')


if 'features' in theJSON:

for i in theJSON['features']:

if i['properties']['mag'] >= 4.0:

print('%2.1f' % i['properties']['mag'], i['properties']['place'])


print()


for i in theJSON['features']:

feltReports = i['properties']['felt']

if feltReports != None and feltReports > 0:

print('%2.1f' % i['properties']['mag'], i['properties']['place'], 'reported', str(feltReports), 'times')


urlData = 'http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson'

webUrl = urllib.request.urlopen(urlData)

print('Server return code:', webUrl.getcode())


if webUrl.getcode() == 200:

data = webUrl.read()

printResule(data)

else:

print('Received an error from server: ', str(webUrl.getcode()))


결과)

Server return code: 200

USGS Magnitude 2.5+ Earthquakes, Past Day

36 events recorded.

4.9 43km E of Nishinoomote, Japan

4.7 160km SSW of Suva, Fiji

4.3 46km ESE of Curico, Chile

4.0 93km SE of Yunaska Island, Alaska

4.2 171km NNW of Saumlaki, Indonesia

4.7 54km W of Bamboo Flat, India

5.0 104km SSW of Banda Aceh, Indonesia

5.3 98km SSW of Taron, Papua New Guinea

4.8 Northern Mid-Atlantic Ridge

4.9 Northern Mid-Atlantic Ridge

4.1 41km N of Murghob, Tajikistan

5.7 87km SW of Hihifo, Tonga

5.3 82km WSW of Khuzdar, Pakistan


3.1 14km NE of Cherokee, Oklahoma reported 1 times

2.5 9km SSE of Gilroy, California reported 1 times

2.5 25km SE of Shingletown, California reported 4 times

3.2 47km NW of Lordsburg, New Mexico reported 8 times

3.1 38km N of Yucca Valley, California reported 2 times

4.7 160km SSW of Suva, Fiji reported 1 times

2.9 109km N of Dorado, Puerto Rico reported 1 times

2.8 8km NW of Healdton, Oklahoma reported 1 times

2.7 28km ENE of Pablo, Montana reported 1 times

2.9 15km SW of Cherokee, Oklahoma reported 1 times


* json 데이터 포맷

http://earthquake.usgs.gov/earthquakes/feed/v1.0/geojson.php

  • html module

# html module

: samplehtml.html

<!DOCTYPE html>

<html lang="en">

  <head>

    <meta charset="utf-8" />

    <title>Sample HTML Document</title>

    <meta name="description" content="This is a sample HTML file" />

    <meta name="author" content="Administrator" />

    <meta name="viewport" content="width=device-width; initial-scale=1.0" />

    <!-- Replace favicon.ico & apple-touch-icon.png in the root of your domain and delete these references -->

    <link rel="shortcut icon" href="/favicon.ico" />

    <link rel="apple-touch-icon" href="/apple-touch-icon.png" />

  </head>


  <body>

    <div>

      <header>

        <h1>HTML Sample File</h1>

      </header>

      <nav>

        <p>

          <a href="/">Home</a>

        </p>

        <p>

          <a href="/contact">Contact</a>

        </p>

      </nav>

      <div>


      </div>

      <footer>

        <p>&copy; Copyright by Administrator</p>

      </footer>

    </div>

  </body>

</html>


# html 파일 파싱
from html.parser import HTMLParser

metacount = 0

class MyHTMLParser(HTMLParser):
''' overriding '''
def handle_starttag(self, tag, attrs):
global metacount
print('Encounted a start tag', tag)
if tag == 'meta':
metacount += 1
pos = self.getpos()
print('At line:', pos[0], 'position', pos[1])
if len(attrs) > 0:
print('\tAttributes:')
for a in attrs:
print('\t', a[0], '=', a[1])

def handle_endtag(self, data):
print('Encounted an end tag:', data)
pos = self.getpos()
print('At line:', pos[0], 'position', pos[1])

def handle_data(self, data):
print('Encounted some data:', data)
pos = self.getpos()
print('At line:', pos[0], 'position', pos[1])
def handle_comment(self, data):
print('Encounted comment:', data)
pos = self.getpos()
print('At line:', pos[0], 'position', pos[1])

parser = MyHTMLParser()

f = open('samplehtml.html', 'r')
contents = f.read()
parser.feed(contents)

print('%d meta tags encounted' % metacount)

결과)
Encounted some data: 

At line: 1 position 15
Encounted a start tag html
At line: 2 position 0
Attributes:
lang = en
Encounted some data: 
  
At line: 2 position 16
Encounted a start tag head
At line: 3 position 2
Encounted some data: 
    
At line: 3 position 8
Encounted a start tag meta
At line: 4 position 4
Attributes:
charset = utf-8
Encounted an end tag: meta
At line: 4 position 4
Encounted some data: 
    
At line: 4 position 28
Encounted a start tag title
At line: 5 position 4
Encounted some data: Sample HTML Document
At line: 5 position 11
Encounted an end tag: title
At line: 5 position 31
Encounted some data: 
    
At line: 5 position 39
Encounted a start tag meta
At line: 6 position 4
Attributes:
name = description
content = This is a sample HTML file
Encounted an end tag: meta
At line: 6 position 4
Encounted some data: 
    
At line: 6 position 68
Encounted a start tag meta
At line: 7 position 4
Attributes:
name = author
content = Administrator
Encounted an end tag: meta
At line: 7 position 4
Encounted some data: 
    
At line: 7 position 50
Encounted a start tag meta
At line: 8 position 4
Attributes:
name = viewport
content = width=device-width; initial-scale=1.0
Encounted an end tag: meta
At line: 8 position 4
Encounted some data: 
    
At line: 8 position 76
Encounted comment:  Replace favicon.ico & apple-touch-icon.png in the root of your domain and delete these references 
At line: 9 position 4
Encounted some data: 
    
At line: 9 position 110
Encounted a start tag link
At line: 10 position 4
Attributes:
rel = shortcut icon
href = /favicon.ico
Encounted an end tag: link
At line: 10 position 4
Encounted some data: 
    
At line: 10 position 52
Encounted a start tag link
At line: 11 position 4
Attributes:
rel = apple-touch-icon
href = /apple-touch-icon.png
Encounted an end tag: link
At line: 11 position 4
Encounted some data: 
  
At line: 11 position 64
Encounted an end tag: head
At line: 12 position 2
Encounted some data: 

  
At line: 12 position 9
Encounted a start tag body
At line: 14 position 2
Encounted some data: 
    
At line: 14 position 8
Encounted a start tag div
At line: 15 position 4
Encounted some data: 
      
At line: 15 position 9
Encounted a start tag header
At line: 16 position 6
Encounted some data: 
        
At line: 16 position 14
Encounted a start tag h1
At line: 17 position 8
Encounted some data: HTML Sample File
At line: 17 position 12
Encounted an end tag: h1
At line: 17 position 28
Encounted some data: 
      
At line: 17 position 33
Encounted an end tag: header
At line: 18 position 6
Encounted some data: 
      
At line: 18 position 15
Encounted a start tag nav
At line: 19 position 6
Encounted some data: 
        
At line: 19 position 11
Encounted a start tag p
At line: 20 position 8
Encounted some data: 
          
At line: 20 position 11
Encounted a start tag a
At line: 21 position 10
Attributes:
href = /
Encounted some data: Home
At line: 21 position 22
Encounted an end tag: a
At line: 21 position 26
Encounted some data: 
        
At line: 21 position 30
Encounted an end tag: p
At line: 22 position 8
Encounted some data: 
        
At line: 22 position 12
Encounted a start tag p
At line: 23 position 8
Encounted some data: 
          
At line: 23 position 11
Encounted a start tag a
At line: 24 position 10
Attributes:
href = /contact
Encounted some data: Contact
At line: 24 position 29
Encounted an end tag: a
At line: 24 position 36
Encounted some data: 
        
At line: 24 position 40
Encounted an end tag: p
At line: 25 position 8
Encounted some data: 
      
At line: 25 position 12
Encounted an end tag: nav
At line: 26 position 6
Encounted some data: 
      
At line: 26 position 12
Encounted a start tag div
At line: 27 position 6
Encounted some data: 

      
At line: 27 position 11
Encounted an end tag: div
At line: 29 position 6
Encounted some data: 
      
At line: 29 position 12
Encounted a start tag footer
At line: 30 position 6
Encounted some data: 
        
At line: 30 position 14
Encounted a start tag p
At line: 31 position 8
Encounted some data:  Copyright by Administrator
At line: 31 position 17
Encounted an end tag: p
At line: 31 position 44
Encounted some data: 
      
At line: 31 position 48
Encounted an end tag: footer
At line: 32 position 6
Encounted some data: 
    
At line: 32 position 15
Encounted an end tag: div
At line: 33 position 4
Encounted some data: 
  
At line: 33 position 10
Encounted an end tag: body
At line: 34 position 2
Encounted some data: 

At line: 34 position 9
Encounted an end tag: html
At line: 35 position 0
Encounted some data: 

At line: 35 position 7

4 meta tags encounted
  • xml module

# xml 데이터 읽기

: samplexml.xml

<?xml version="1.0" encoding="UTF-8" ?>

<person>

  <firstname>Joe</firstname>

  <lastname>Marini</lastname>

  <home>San Francisco</home>

  <skill name="JavaScript"/>

  <skill name="Python"/>

  <skill name="C#"/>

  <skill name="HTML"/>

</person>


# xml 파일파싱
import xml.dom.minidom

doc = xml.dom.minidom.parse('samplexml.xml')

print(doc.nodeName)
print(doc.firstChild.tagName)

skills = doc.getElementsByTagName('skill')
print('%d skills:' % skills.length)
for skill in skills:
print(skill.getAttribute('name'))

newSkill = doc.createElement('skill')
newSkill.setAttribute('name', 'jQuery')
doc.firstChild.appendChild(newSkill)

print()
skills = doc.getElementsByTagName('skill')
print('%d skills:' % skills.length)
for skill in skills:
print(skill.getAttribute('name'))

결과)
#document
person
4 skills:
JavaScript
Python
C#
HTML

5 skills:
JavaScript
Python
C#
HTML
jQuery


반응형

관련글 더보기

댓글 영역