1 / 23

Python for XML Processing and Web Applications

Python for XML Processing and Web Applications. 資管所 碩一 R97725009 廖 耘 資管所 碩一 R97725023 曾子豪. Outline. Introduction to Python ( 廖耘 ) Basic XML Processing-DOM ( 曾子豪 ) Parsing HTML ( 曾子豪 ) Web Application Using Python ( 曾子豪 ) The Power of Python and XML ( 廖耘 ). Introduction to Python.

aleron
Download Presentation

Python for XML Processing and Web Applications

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Python for XML Processing and Web Applications 資管所 碩一 R97725009 廖 耘 資管所 碩一 R97725023 曾子豪

  2. Outline • Introduction to Python (廖耘) • Basic XML Processing-DOM (曾子豪) • Parsing HTML (曾子豪) • Web Application Using Python (曾子豪) • The Power of Python and XML (廖耘)

  3. Introduction to Python • Python:一款簡單強大又好用的動態語言 • 簡單易學 • 可讀性佳 • 眾多的社群與豐富的函式庫 • 物件導向 • 動態語言 • 跨平台 • 容易擴充和嵌入 • 廣泛使用 • 良好的文件

  4. Introduction to Python • Sample Code: Hello World! """This is a sample code of python"""哈囉 世界! a = u'哈囉 世界!' print a • Sample Code: Output a 9*9 multiplication table #This is a sample code of python九九乘法表 for i in range(1, 10): for j in range(1, 10): print "%d * %d = %d" %(i, j, i*j) print

  5. Introduction to Python • Sample Code: Function Declaration def say_hello(): print 'Hello World!' # body of the function say_hello() # calling the function • Sample Code: Call C programs in python """Sample code: import a pre-compiled c program into python script""" import ctypes #object to access foreign library importedCFunction=ctypes.CDLL("./a.out") #import importedCFunction.Cfunction( ... ) #access foreign function

  6. Introduction to Python • Sample Code: OOP class Person: def __init__(self, name): self.name = name def say_hi(self): print 'Hello, my name is', self.name p = Person('Swaroop') p.say_hi() • Inheritance: class Man(Person): …

  7. Basic XML processing-DOM • Create a Document and a school element with NS (d1.py) import xml.dom.minidom def get_a_document(): doc = xml.dom.minidom.Document() school_element = doc.createElementNS("http://www.ntu.edu.tw/Eric/school", "school") doc.appendChild(school_element) return doc, school_element

  8. Basic XML processing-DOM(2) • Create a location element appended on school element (d2.py) def add_a_location(doc, school_element): location_element = doc.createElementNS("http://www.ntu.edu.tw/Eric/school", "location") school_element.appendChild(location_element) return location_element

  9. Basic XML processing-DOM(3) Create a surrounding element appended on location element and a text node appended on surrounding element

  10. Basic XML processing-DOM(3) (d3.py) def add_surroundings(doc, location_element): surroundings_element = doc.createElementNS("http://www.ntu.edu.tw/Eric/school", "surroundings") location_element.appendChild(surroundings_element) description = doc.createTextNode("A place with many handsome boys and beautiful girls.") surroundings_element.appendChild(description) return surroundings_element

  11. Basic XML processing-DOM(4) • Create another text node appended on surrounding element (d4.py) def add_more_surroundings(doc, surroundings_element): description = doc.createTextNode("But it's usually raining there. Damn!!!!!!!!!!.")surroundings_element.appendChild(description)

  12. Basic XML processing-DOM(5) • We can combine the two text nodes (d5.py) def fix_element(element): element.normalize()

  13. Basic XML processing-DOM(6) • We then write the result on screen (write_doc.py) import xml.dom.ext import xml.dom.minidom def write_to_screen(doc): xml.dom.ext.PrettyPrint(doc)

  14. Basic XML processing-DOM(7)

  15. Parsing HTML # -*- coding: utf-8 -*- import urllib # 抓取Yahoo!奇摩天氣的網頁內容放到WebContent weatherWeb = urllib.urlopen("http://tw.weather.yahoo.com/today.html") webContent = weatherWeb.read().decode('utf_8') weatherWeb.close()

  16. Parsing HTML import HTMLParser # 用來解析天氣資訊的解析器,繼承自HTMLParser class WeaterHTMLParser(HTMLParser.HTMLParser): def handle_data(self, data): """處理標籤以外的資料,也就是網頁中的文字""" data = data.strip() # 如果台中地區已出現過,就記錄天氣資訊 if hasattr(self, 'found') and data: # 到了彰化地區,中止解析 if data == u'彰化地區': self.stop = True return self.weather.append(data) # 出現台中地區,設定旗標以通知後面幾次呼叫記下資訊 if data == u'台中地區': self.found = True self.weather = [] self.weather.append(data) def unknown_decl(self, data): """Override unknown handle method to avoid exception""" pass

  17. Parsing HTML Parser = WeaterHTMLParser() try: # 將網頁內容拆成一行一行餵給Parser for line in webContent.splitlines(): # 如果出現停止旗標,停止餵食資料,並且跳出迴圈 if hasattr(Parser, 'stop') and Parser.stop: break Parser.feed(line) except HTMLParser.HTMLParseError, data: print "# Parser error : " + data.msg Parser.close() print u"%s 今天的天氣是 %s,氣溫是 %s度%s,降雨機率是 %s" % tuple(Parser.weather)

  18. Web application using Python Zope:一套以 Python 撰寫的Web application server 框架,提供網站伺服器、資料庫、會員與資料管理、搜尋等功能。(後台資料管理) Plone:一套CMS(Content Management System),網頁式的應用軟體 (前台內容管理)

  19. Python, Zope, Plone 關係圖

  20. ZMI (Zope Management Interface)

  21. The Power of Python and XML • 對Unicode的強大支援 • 內建極強(極快!)的XML Parser • 程式碼易於閱讀及維護 • 互動式直譯器易於測試 • 跨平台 • 物件導向

  22. Thank You!

More Related