标签:
I ran across this warning:
#!/usr/bin/env python2.3
VAR = ‘xxx‘
if __name__ == ‘__main__‘:
global VAR
VAR = ‘yyy‘
---
OUTPUT:
./var.py:0: SyntaxWarning: name ‘VAR‘ is assigned to before global declaration
----
But, a little twiddle quiets the warning, and I have no idea why:
#!/usr/bin/env python2.3
VAR = ‘xxx‘
def set_var():
global VAR
VAR = ‘yyy‘
if __name__ == ‘__main__‘:
set_var()
---
No output.
Global is normally used within a function definition to allow it to assign
to names defined outside the function (as in your 2nd example). In your
first example global is outside any function definition, and therefore not
meaningful, as well as giving a SyntaxWarning.
HTMLParser的feed()方法会调用
handle_starttag(), handle_data(), handle_endtag()方法
#! /usr/bin/env python #coding=utf-8 from htmlentitydefs import entitydefs from HTMLParser import HTMLParser import sys class TitleParser(HTMLParser): def __init__(self): self.title = ‘ ‘ self.readingtitle = 0 HTMLParser.__init__(self) def handle_starttag(self, tag, attrs): if tag == ‘title‘: self.readingtitle = 1 def handle_data(self, data): if self.readingtitle: self.title += data def handle_endtag(self, tag): if tag == ‘title‘: self.readingtitle = 0 def handle_entityref(self, name): if entitydefs.has_key(name): self.handle_data(entitydefs[name]) else: self.handle_data(‘&‘ + name + ‘;‘) def gettitle(self): return self.title fd = open(sys.argv[1]) tp = TitleParser() tp.feed(fd.read()) print "Title is:", tp.gettitle()
HTMLParser是python用来解析html的模块。它可以分析出html里面的标签、数据等等,是一种处理html的简便途径。 HTMLParser采用的是一种事件驱动的模式,当TMLParser找到一个特定的标记时,它会去调用一个用户定义的函数,以此来通知程序处理。它 主要的用户回调函数的命名都是以handler_开头的,都是HTMLParser的成员函数。当我们使用时,就从HTMLParser派生出新的类,然 后重新定义这几个以handler_开头的函数即可。
handle_startendtag 处理开始标签和结束标签
handle_starttag 处理开始标签,比如<xx>
handle_endtag 处理结束标签,比如</xx>
handle_charref 处理特殊字符串,就是以&#开头的,一般是内码表示的字符
handle_entityref 处理一些特殊字符,以&开头的,比如
handle_data 处理数据,就是<xx>data</xx>中间的那些数据
handle_comment 处理注释
handle_decl 处理<!开头的,比如<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
handle_pi 处理形如<?instruction>的东西
[python] view plaincopyprint?
>>> help(HTMLParser.HTMLParser.handle_endtag)
Help on method handle_endtag in module HTMLParser:
handle_endtag(self, tag) unbound HTMLParser.HTMLParser method
# Overridable -- handle end tag
>>> help(HTMLParser.HTMLParser.handle_data)
Help on method handle_data in module HTMLParser:
handle_data(self, data) unbound HTMLParser.HTMLParser method
# Overridable -- handle data
>>> help(HTMLParser.HTMLParser.handle_charref)
Help on method handle_charref in module HTMLParser:
handle_charref(self, name) unbound HTMLParser.HTMLParser method
# Overridable -- handle character reference
>>> help(HTMLParser.HTMLParser.handle_decl)
Help on method handle_decl in module HTMLParser:
handle_decl(self, decl) unbound HTMLParser.HTMLParser method
# Overridable -- handle declaration
>>> help(HTMLParser.HTMLParser.handle_startendtag)
Help on method handle_startendtag in module HTMLParser:
handle_startendtag(self, tag, attrs) unbound HTMLParser.HTMLParser method
# Overridable -- finish processing of start+end tag: <tag.../>
>>> help(HTMLParser.HTMLParser.handle_endtag)
Help on method handle_endtag in module HTMLParser:
handle_endtag(self, tag) unbound HTMLParser.HTMLParser method
# Overridable -- handle end tag
>>> help(HTMLParser.HTMLParser.handle_data)
Help on method handle_data in module HTMLParser:
handle_data(self, data) unbound HTMLParser.HTMLParser method
# Overridable -- handle data
>>> help(HTMLParser.HTMLParser.handle_charref)
Help on method handle_charref in module HTMLParser:
handle_charref(self, name) unbound HTMLParser.HTMLParser method
# Overridable -- handle character reference
>>> help(HTMLParser.HTMLParser.handle_decl)
Help on method handle_decl in module HTMLParser:
handle_decl(self, decl) unbound HTMLParser.HTMLParser method
# Overridable -- handle declaration
>>> help(HTMLParser.HTMLParser.handle_startendtag)
Help on method handle_startendtag in module HTMLParser:
handle_startendtag(self, tag, attrs) unbound HTMLParser.HTMLParser method
# Overridable -- finish processing of start+end tag: <tag.../>
标签:
原文地址:http://www.cnblogs.com/alansheng/p/4636072.html