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

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

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("
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'
4

2 Answers

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'})

1
import urllib.request
searchStr = input("Enter Search Query \n")
r = urllib.request.urlopen("
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)

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like