#* * PLASTIC to ASCII!!! **definttoascii (size):Importbinascii A16=hex (int (size)). Lstrip ("0x") forXinchRange (len (A16), 4): A16='0'+A16returnBinascii.b2a_hex (A16)#* * Fill 0!!! **defaddzone (size): A8=Str (size) forXinchRange (len (size)), 8): A8=STR ('0'+A8)returnA8
#* * Filter tags in HTML * *deffilter_tags (HTMLSTR):ImportRe#Filter CDATA FirstRe_cdata=re.compile ('//<!\[cdata\[[^>]*//\]\]>', Re. I)#Match CDATARe_script=re.compile ('<\s*script[^>]*>[^<]*<\s*/\s*script\s*>', Re. I)#ScriptRe_style=re.compile ('<\s*style[^>]*>[^<]*<\s*/\s*style\s*>', Re. I)#styleRe_br=re.compile ('<br\s*?/?>')#Handling line breaksRe_h=re.compile ('</?\w+[^>]*>')#HTML TagsRe_comment=re.compile ('<!--[^>]*-->')#HTML AnnotationsS=re_cdata.sub ("', HTMLSTR)#Remove CDATAS=re_script.sub ("', s)#Remove ScriptS=re_style.sub ("', s)#Remove StyleS=re_br.sub ("', s)#convert BR to line breakS=re_h.sub ("', s)#Remove HTML TagsS=re_comment.sub ("', s)#Remove HTML Annotations #remove the extra empty linesBlank_line=re.compile ('\n+') s=blank_line.sub ('\ n', s) s=replacecharentity (s)#Replace entity returns#* * Replace entities in HTML * *defreplacecharentity (HTMLSTR):Importre char_entities={'nbsp':' ',' the':' ', 'LT':'<',' -':'<', 'GT':'>',' +':'>', 'amp':'&',' -':'&', 'quot':'"',' the':'"',} re_charentity=re.compile (R'? (? p<name>\w+);') SZ=Re_charentity.search (HTMLSTR) whilesz:entity=sz.group ()#entity full name, such as >Key=sz.group ('name')#remove &; After entity, such as > for GT Try: Htmlstr=re_charentity.sub (char_entities[key],htmlstr,1) SZ=Re_charentity.search (HTMLSTR)exceptKeyerror:#Replace with an empty stringHtmlstr=re_charentity.sub ("', htmlstr,1) SZ=Re_charentity.search (HTMLSTR)returnHtmlstr
#* * Determine if SET * *defisset (variable):ifvariable:returnTruereturnFalse#time stamp Turn timedefDate (unixtime, format ='%y-%m-%d%h:%m'): Importdatetime d=Datetime.datetime.fromtimestamp (unixtime)returnD.strftime (format)
#Download HTTP image return picture namedefdownimg (imgurl):ImportUrllib2ImportOSImportUUID URL=Imgurl Exten=url.split ('.') name="/var/www/html/downimg/"+str (date (), format ='%y%m%d'))+'/'+str (UUID.UUID1 ()) +"."+exten[3] FileD="/var/www/html/downimg/"+str (date (), format ='%y%m%d'))+'/' if notOs.path.exists (FileD):#determine if a folder existsos.makedirs (FileD)#note the type to match when saving the file, if the picture you want to save is JPG, the name of the open file must be in JPG format, otherwise an invalid picture will be generated .conn =urllib2.urlopen (URL) F= Open (name,'WB') F.write (Conn.read ()) F.close ()returnname#microsecond Timedefmicrotime ():Import TimeImportMathreturn '%f%d'% MATH.MODF (Time.time ())
Python summarizes common processing functions