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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Atrisa
    New Member
    • Sep 2010
    • 22

    #1

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

    I have a file with lines like this:

    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
    With the following code:

    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
    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:

    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}
    But it gives me the following error:

    Code:
    AttributeError: 'NoneType' object has no attribute 'split'
    Can anyone please tell what am I doing wrong here?
    Thanks in advance, Atrisa
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    urlparse(ref).h ostname returns None when hostname is not present. urlparse is interpreting the string "13.gateway.edg e.messenger.liv e.com/help" as path.

    Comment

    • Atrisa
      New Member
      • Sep 2010
      • 22

      #3
      I changed
      Code:
      urlparse(ref).hostname
      to
      Code:
      urlparse(ref).netloc
      , now it neither gives error nor prints anything. Is there anything else that could do what I want
      Code:
      hostname
      to do?

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Parse the string manually. What possible variations of the string do you expect?

        Comment

        • Atrisa
          New Member
          • Sep 2010
          • 22

          #5
          I could solve the problem by using
          Code:
          netloc
          instead of
          Code:
          hostname
          and then some manual parsing. Thanks bvdet.

          Comment

          Working...