Shuhratjon Zikiryaev
Shuhratjon Zikiryaev

Reputation: 11

AttributeError: 'NoneType' object has no attribute 'text' -

My code

for page in pages:
  req = requests.get("https://nvd.nist.gov/vuln/detail/{}".format(page)).text
  page_soup = soup(req, "html.parser")
  descr = page_soup.findAll('p', {'data-testid':'vuln-description'})[0].text
  version = page_soup.find('a', {'id': 'Cvss2CalculatorAnchor'}).text
  title = page_soup.find('span', {'data-testid': 'page-header-vuln-id'}).text
  try:
    print(descr)
    print(version)
    print(title)
  
  except AttributeError:
    continue



AttributeError                            Traceback (most recent call last)
<ipython-input-14-9fa6e71d70d5> in <module>()
      3   page_soup = soup(req, "html.parser")
      4   descr = page_soup.findAll('p', {'data-testid':'vuln-description'})[0].text
----> 5   version = page_soup.find('a', {'id': 'Cvss2CalculatorAnchor'}).text
      6   title = page_soup.find('span', {'data-testid': 'page-header-vuln-id'}).text
      7   try:

AttributeError: 'NoneType' object has no attribute 'text'

I am trying to collect data, some of the data points do not have the actual data so I wanted to skip it. Even tho I am using "try-except" I am still getting that error in the middle of my collection.

Upvotes: 0

Views: 668

Answers (1)

adamgy
adamgy

Reputation: 5613

Put the lines of code you're expecting an error from inside of the try block:

for page in pages:
  req = requests.get("https://nvd.nist.gov/vuln/detail/{}".format(page)).text
  page_soup = soup(req, "html.parser")
  try:
    descr = page_soup.findAll('p', {'data-testid':'vuln-description'})[0].text
    version = page_soup.find('a', {'id': 'Cvss2CalculatorAnchor'}).text
    title = page_soup.find('span', {'data-testid': 'page-header-vuln-id'}).text
    print(descr)
    print(version)
    print(title)
  
  except AttributeError:
    continue

Upvotes: 3

Related Questions