-8

I am trying to get href link from a tag but when I use method .get it gives me error "non type object has no attribute get" here is my code:

#Loading Libraries
import urllib
import urllib.request
from bs4 import BeautifulSoup

#define URL for scraping
theurl = "http://www.techspot.com/"
thepage = urllib.request.urlopen(theurl)

#Cooking the Soup
soup = BeautifulSoup(thepage,"html.parser")
i=1
#Scraping "Project Title" (project-title)
title = soup.findAll('div', {'class': 'article-category'})
for titles in title:

    titleheading = soup.findAll('h2')
    for titletext in titleheading:
        titlename = titletext.a
        titlelink =titlename.get('href')
        print(i)
        print(titlelink)
        i+=1

and here is the console screenshot of the error I am getting

enter image description here

Tell me what's the problem in this code or why i am getting this error

depperm
  • 10,606
  • 4
  • 43
  • 67
Hassan Raza
  • 45
  • 10
  • titlelink is None – depperm Jan 18 '17 at 17:55
  • 3
    Possible duplicate of [Python: Attribute Error - 'NoneType' object has no attribute 'something'](http://stackoverflow.com/questions/8949252/python-attribute-error-nonetype-object-has-no-attribute-something) – That1Guy Jan 18 '17 at 18:00

1 Answers1

0

You need to check if titletext.a is None before you can use it for sure.

for titles in title:
    titleheading = soup.findAll('h2')
    for titletext in titleheading:
        if titletext.a:
            titlename = titletext.a
            titlelink =titlename.get('href')
            print(i)
            print(titlelink)
        i+=1
denvaar
  • 2,174
  • 2
  • 22
  • 26