I have a file with lines like this:
With the following code:
I try to take the referer,the sites have been referred from the referrer and the count of each of the sites and put them in a dictionary. Then for sites that after resplitting by (.) is longer than 3 or ends in an alphabetical letter, take the last 3 words of it, to have a result like this:
But it gives me the following error:
Can anyone please tell what am I doing wrong here?
Thanks in advance, Atrisa
Code:
Host: gateway.edge.messenger.live.com Referer: 13.gateway.edge.messenger.live.com/help Host: ads.lfstmedia.com Referer: http://facebook.mafiawars.com:80/? Host: gateway.edge.messenger.live.com Referer: 13.gateway.edge.messenger.live.com/help Host: ads.lfstmedia.com Referer: 13.gateway.edge.messenger.live.com/help Host: ads.google.com Referer: 16.17.18.19
Code:
refhost = dict()
for line in open('referer.txt'):
if line.startswith('Host'):
host = line.split(': ')[1].strip('\n')
elif line.startswith('Referer'):
ref = line.split(': ')[1].strip('\n')
refer = urlparse(ref).hostname.split('.')
if (len(refer) > 3 and refer[-1].isalpha()):
referrer = tuple(refer[-3:])
refhost.setdefault(referrer, dict()).setdefault(host, 0)
refhost[referrer][host] +=1
Code:
messenger.live.com {'gateway.edge.messenger.live.com': 2, 'ads.lfstmedia.com': 1}
facebook.mafiawars.com {'ads.lfstmedia.com': 1}
16.17.18.19 {'ads.google.com': 1}
Code:
AttributeError: 'NoneType' object has no attribute 'split'
Thanks in advance, Atrisa
Comment