Python之HTMLParser
"""A parser for HTML and XHTML."""
# This file is based on sgmllib.py, but the API is slightly different.
# XXX There should be a way to distinguish between PCDATA (parsed
# character data -- the normal case), RCDATA (replaceable character
# data -- only char and entity references and end tags are special)
# and CDATA (character data -- only end tags are special).
import _markupbase
import re
# Regular expressions used for parsing
interesting_normal = re.compile('[&<]')
interesting_cdata = re.compile(r'<(/|\Z)')
incomplete = re.compile('&[a-zA-Z#]')
entityref = re.compile('&([a-zA-Z][-.a-zA-Z0-9]*)[^a-zA-Z0-9]')
charref = re.compile('&#(?:[0-9]+|[xX][0-9a-fA-F]+)[^0-9a-fA-F]')
starttagopen = re.compile('<[a-zA-Z]')
piclose = re.compile('>')
commentclose = re.compile(r'--\s*>')
tagfind = re.compile('[a-zA-Z][-.a-zA-Z0-9:_]*')
attrfind = re.compile(
r'\s*([a-zA-Z_][-.:a-zA-Z_0-9]*)(\s*=\s*'
r'(\'[^\']*\'|"[^"]*"|[-a-zA-Z0-9./,:;+*%?!&$\(\)_#=~@]*))?')
locatestarttagend = re.compile(r"""
<[a-zA-Z][-.a-zA-Z0-9:_]* # tag name
(?:\s+ # whitespace before attribute name
(?:[a-zA-Z_][-.:a-zA-Z0-9_]* # attribute name
(?:\s*=\s* # value indicator
(?:'[^']*' # LITA-enclosed value
|\"[^\"]*\" # LIT-enclosed value
|[^'\">\s]+ # bare value
)
)?
)
)*
\s* # trailing whitespace
""", re.VERBOSE)
endendtag = re.compile('>')
endtagfind = re.compile('</\s*([a-zA-Z][-.a-zA-Z0-9:_]*)\s*>')
class HTMLParseError(Exception):
"""Exception raised for all parse errors."""
def __init__(self, msg, position=(None, None)):
assert msg
self.msg = msg
self.lineno = position[0]
self.offset = position[1]
def __str__(self):
result = self.msg
相关文档:
上一篇中我们已经了解如何在Python程序和C模块之间进行值的相互传递,现在我们来进入实作阶段,看看如何将一个C语言开发的开源mp3编解码库LAME包装为一个Python下可以使用的扩展模块。首先去http://lame.sourceforge.net/download.php下载LAME的源代码,然后切换到root用户编译源代码,./configure
make
make instal ......
>>> a="abcd"
>>> ",".join(a)
'a,b,c,d'
>>> "|".join(['a','b','c'])
'a|b|c'
>>> ",".join(('a','b','c'))
'a,b,c'
>>> ",".join({'a':1,'b':2,'c':3})
'a,c,b' ......
eval(str [,globals [,locals ]])函数将字符串str当成有效Python表达式来求值,并返回计算结果。
同样地, exec语句将字符串str当成有效Python代码来执行.提供给exec的代码的名称空间和exec语句的名称空间相同.
最后,execfile(filename [,globals [,locals ]])函数可以用来执行一个文件,看下面的例子:
>>> ev ......
如果你对Python矩阵转置的实际应用操作方案的转置不知道如何进行下一步时,你就你需要转置一个二维数组,将Python矩阵转置的行列互换.
这样就可以完成你所需要的应用操作,以下是文章的具体操作。
你需要转置一个二维数组,将行列互换,讨论:你需要确保该数组的行列数都是相同的.比如:
arr
= [[1, 2,&nbs ......
这个脚本是在 python 环境下使用的,改的网上的一个脚本,可以检测代理中国(www.proxycn.com)上的HTTP代理列表,你也可以自己去上面找列表检测 代码: #!/usr/bin/python # -*- coding: utf-8 -*- # from: ubuntu.org.cn Copyright: GPLv2 import urllib import re from datetime import datetime import socket def fin ......