By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,908 Members | 1,860 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

How to fix error "'NoneType' object has no attribute 'split'"?

P: 22
I have a file with lines like this:

Expand|Select|Wrap|Line Numbers
  1. Host: gateway.edge.messenger.live.com 
  2. Referer: 13.gateway.edge.messenger.live.com/help 
  3. Host: ads.lfstmedia.com
  4. Referer: http://facebook.mafiawars.com:80/?
  5. Host: gateway.edge.messenger.live.com 
  6. Referer: 13.gateway.edge.messenger.live.com/help 
  7. Host: ads.lfstmedia.com
  8. Referer: 13.gateway.edge.messenger.live.com/help
  9. Host: ads.google.com
  10. Referer: 16.17.18.19
  11.  
With the following code:

Expand|Select|Wrap|Line Numbers
  1. refhost = dict()
  2.  
  3. for line in open('referer.txt'):
  4.     if line.startswith('Host'):
  5.         host = line.split(': ')[1].strip('\n')
  6.     elif line.startswith('Referer'):
  7.         ref = line.split(': ')[1].strip('\n')
  8.     refer = urlparse(ref).hostname.split('.')
  9.         if (len(refer) > 3 and refer[-1].isalpha()):
  10.             referrer = tuple(refer[-3:])       
  11.             refhost.setdefault(referrer, dict()).setdefault(host, 0)
  12.             refhost[referrer][host] +=1
  13.  
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:

Expand|Select|Wrap|Line Numbers
  1. messenger.live.com {'gateway.edge.messenger.live.com': 2, 'ads.lfstmedia.com': 1}
  2. facebook.mafiawars.com {'ads.lfstmedia.com': 1} 
  3. 16.17.18.19 {'ads.google.com': 1}
  4.  
But it gives me the following error:

Expand|Select|Wrap|Line Numbers
  1. AttributeError: 'NoneType' object has no attribute 'split'
Can anyone please tell what am I doing wrong here?
Thanks in advance, Atrisa
Jan 25 '11 #1

✓ answered by bvdet

urlparse(ref).hostname returns None when hostname is not present. urlparse is interpreting the string "13.gateway.edge.messenger.live.com/help" as path.

Share this Question
Share on Google+
4 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
urlparse(ref).hostname returns None when hostname is not present. urlparse is interpreting the string "13.gateway.edge.messenger.live.com/help" as path.
Jan 25 '11 #2

P: 22
I changed
Expand|Select|Wrap|Line Numbers
  1. urlparse(ref).hostname
to
Expand|Select|Wrap|Line Numbers
  1. urlparse(ref).netloc
, now it neither gives error nor prints anything. Is there anything else that could do what I want
Expand|Select|Wrap|Line Numbers
  1. hostname
to do?
Jan 25 '11 #3

bvdet
Expert Mod 2.5K+
P: 2,851
Parse the string manually. What possible variations of the string do you expect?
Jan 25 '11 #4

P: 22
I could solve the problem by using
Expand|Select|Wrap|Line Numbers
  1. netloc
instead of
Expand|Select|Wrap|Line Numbers
  1. hostname
and then some manual parsing. Thanks bvdet.
Jan 26 '11 #5

Post your reply

Sign in to post your reply or Sign up for a free account.