Advertisement
dequone

parse url (full)

Sep 2nd, 2014
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.09 KB | None | 0 0
  1. def parseLink(string):
  2.     global out
  3.     buf = {}
  4.  
  5.     string = string.replace("http://", "")
  6.     string = string.replace("https://", "")
  7.     string = string.replace("\n", "")   #lin/mac
  8.     string = string.replace("\r", "")
  9.     string = string.replace("\r\n", "") #win
  10.  
  11.     i=j=0
  12.     dp1=dp2=0
  13.     domen=''
  14.    
  15.     while i < len(string):
  16.         #1 - get domen
  17.         while domen == '':
  18.             if string[i]=='.':
  19.                 if dp1==0:
  20.                     dp1 = i
  21.                 elif dp1 != 0 and dp1 != i and dp2 == 0:
  22.                     dp2 = i
  23.                 elif dp2 != 0 and dp2 != i:
  24.                     dp1 = dp2
  25.                     dp2 = i
  26.             elif string[i]=='/':
  27.                 if dp2 != 0:
  28.                     domen = string[dp1+1:i]
  29.                 else:
  30.                     domen = string[0:i]
  31.                 #print '<domen>: ', domen, '\n<forms>:'
  32.                 buf[domen] = []
  33.                 break
  34.             i += 1
  35.  
  36.         #2 - get every form          
  37.         if string[i] == '/' and j == 0:
  38.             j = i
  39.             while string[i] != '=' and i < len(string):
  40.                 if string[i] in '/?&':
  41.                     j = i
  42.                 i += 1
  43.             #print string[j+1:i]
  44.            
  45.         try:
  46.             if string[i] in '?&':
  47.                 j = i
  48.                 while string[i] != '=' and i < len(string):
  49.                     if string[i] in '?&':
  50.                         j = i
  51.                     i += 1
  52.                 #print string[j+1:i]
  53.  
  54.                 if string[j+1:i] not in buf[domen]:
  55.                     buf[domen].append(string[j+1:i])
  56.  
  57.         except BaseException as ex:
  58.                 print 'Exception 2: ' + str(ex)
  59.  
  60.         i+=1
  61.            
  62.     print buf
  63.  
  64.     #3 - write in global dict
  65.     if domen in out.keys():
  66.         #for a in buf[domen]:
  67.         if buf[domen] not in out[domen]:
  68.             out[domen].append(a)
  69.     else:
  70.         out[domen] = []
  71.         #for a in buf[domen]:
  72.         out[domen].append(buf[domen])
  73.    
  74.     print '-----------------------------------'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement