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' ......
×î½üÓÖÖØÐÂ×°ÉÏÁËwindows 7¸Ð¾õ»¹ÊÇÄÇÑù£¬Ö÷ÒªÊÇÏëÓÃM8SDKдЩ³ÌÐò¡£Ò²ÏëÔÚwindowsÉÏÍæÍæ£¬Ò»Ö±¶¼¾õµÃÓÃCдһЩ³£ÓõĶ«¶«ºÜ¸´ÔÓ£¬Ö»ÓнèÖúÓÚ½âÊÍÐÔÓïÑÔÁË£¬ÔÚpython, ruby¼äÑ¡Ôñ£¬×îÖÕÓÃPython¡£
pythonµÄÓï·¨·Ç³£¼òµ¥£¬¶ÔÓÚÒ»¸öѧ¹ý±à³ÌµÄÈË¿ÉÄÜÒ»ÉÏÎç¾Í²î²»¶àÁË£¬²»¹ýpythonµÄÇ¿´óÖ®´¦ÔÚÓÚËüÓÐN¶àµÄ¿â¿ÉÒÔ·½±ãÎÒÃÇ´¦ÀíÎ ......
±¾Æª½«½éÉÜpythonÖÐsys, getoptÄ£¿é´¦ÀíÃüÁîÐвÎÊý
Èç¹ûÏë¶Ôpython½Å±¾´«²ÎÊý£¬pythonÖжÔÓ¦µÄargc, argv(cÓïÑÔµÄÃüÁîÐвÎÊý)ÊÇÊ²Ã´ÄØ£¿
ÐèҪģ¿é£ºsys
²ÎÊý¸öÊý£ºlen(sys.argv)
½Å±¾Ãû£º sys.argv[0]
²ÎÊý1£º sys.argv[1]
²ÎÊý2£º sys.argv[2]
test.py
1
import ......
ÒÔÏÂΪÎÒÔÚ´¦ÀíÍøÒ³±àÂëÎÊÌâ¿´µÄÎÄÕ£¬ÏÖÔÚдÅÀ³æ×Ô¼º´¦ÀíÍøÒ³±àÂëÎÊÌâȷʵ±È½ÏÂé·³£¬ÍøÒ³Ò²Ã»Óй涨
Ò»¶¨ÒªÔÚMETAÍ·ÐÅÏ¢Àï´ø±àÂëÐÅÏ¢£¬¶øºÜ¶àÈËдµÄ̽²â±àÂë¾ÍÊÇ»ñÈ¡META£¬ÕâÊDz»¿ÉÈ¡µÄ£¬¶øÓеÄÈËÔòÊÇ·ÖÎöHTTP·µ»ØµÄÊý¾Ý°üÀï±ßµÄ
charset£¬È磨a.headers.getparam('charset')£©£¬¶øHTTP°üûÓй涨һ¶¨Òª´øcharset£¬ËùÒ ......