Python apache log gets Baidu keyword search source
#!/usr/bin/python
#-#-Coding:utf-8-*-
Import OS, base64, RE, Fnmatch, Imghdr, Shutil, Pprint, Urlparse
Log = "SEO tutorial. Log"
reader = open (log, ' R ')
Config = {' S0 ': {' h ': ' www.google.com.hk ', ' Q ': ' Q '}, ' s1 ': {' h ': ' www.baidu.com ', ' Q ': ' Wd|word '}, ' S3 ': {' h ': ' Www.soso.com ', ' Q ': ' W '}}
def get_q (x):
For i,j in Config.items ():
Str_q = j[' q '].split (' | ')
if X.netloc = = j[' h ']:
Return str_q
For line in Reader.xreadlines ():
p = re.compile ('. * "Get (/seo/t.php tutorial?) * http/1.1 ". *", Re.ignorecase)
m = P.match (line)
If M:
s_t = M.group (1)
S_t_u = Urlparse.urlparse (s_t)
S_t_u_qs = Urlparse.parse_qs (s_t_u.query,true)
#print s_t_u_qs[' ref '][0]
ref = Urlparse.urlparse (str (s_t_u_qs[' ref '][0]))
Ref_qs = Urlparse.parse_qs (ref.query,true)
#print ref
#print Get_q (ref)
For k in Get_q (ref):
If k in Ref_qs:
Print ref.netloc+ ":::" +ref_qs[k][0]
Reader.close ()