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

Viewed 32380

I have to search for CVE's or Common Vulnerabilities and Exposures through a search query on a website and then print the import the resulting table in my print request. The website I'm using for scraping of the results are https://cve.mitre.org/cgi-bin/cvekey.cgi?keyword=CVE+2017

I'm using python 3 to code and am new to it and wanted to use this as a project

import urllib.request
import urllib
searchStr = input("Enter Search Query \n")
r = urllib.request.urlopen("https://cve.mitre.org/cgi-bin/cvekey.cgi?
keyword="+searchStr)
source_code = r.read()
from bs4 import BeautifulSoup
soup = BeautifulSoup(source_code, 'html.parser')
table = soup.find('tbody', id = 'TableWithRules')
rows = table.find('tr')
for tr in rows:
    cols = tr.find('td')
    p = cols[0].text.strip()
    d = cols[1].text.strip
    print(p)
    print(d)

Gives me the following error:

Traceback (most recent call last):
  File "C:\Users\Devanshu Misra\Desktop\Python\CVE_Search.py", line 9, in 
<module>
    rows = table.find('tr')
AttributeError: 'NoneType' object has no attribute 'find'
2 Answers
import urllib.request
searchStr = input("Enter Search Query \n")
r = urllib.request.urlopen("https://cve.mitre.org/cgi-bin/cvekey.cgi?
keyword="+searchStr)
source_code = r.read()
from bs4 import BeautifulSoup
soup = BeautifulSoup(source_code, 'html.parser')

# FIRST OF ALL SEE THAT THE ID "TableWithRules" is associated to the divtag 

table = soup.find('div', {"id" : 'TableWithRules'})
rows=table.find_all("tr")   # here you have to use find_all for finding all rows of table
for tr in rows:
    cols = tr.find_all('td') #here also you have to use find_all for finding all columns of current row
    if cols==[]: # This is a sanity check if columns are empty it will jump to next row
        continue
    p = cols[0].text.strip()
    d = cols[1].text.strip()
    print(p)
    print(d)

the 'correct'answer is not correct
this line is wrong:

divtag=soup.find('div',{'id':'TableWithRules'})

It should be:

table=soup.find('div',{'id':'TableWithRules'})

Related