2017-10-05 12 views
-1

ウェブサイト上で検索クエリを使用してCVEやCommon Vulnerabilities and Exposuresを検索し、その結果の表を印刷要求で印刷する必要があります。私は結果をこするために使用しているウェブサイトは、コードへの私のpython 3を使用していhttps://cve.mitre.org/cgi-bin/cvekey.cgi?keyword=CVE+2017AttributeError: 'NoneType'オブジェクトに 'find'属性がありません

あり、それに新しいですし、プロジェクト

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) 

としてこれを使用していた私に、次のエラーが発生します:

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' 
+0

'soup.find( 'TBODY'、ID = 'TableWithRules')' 'NONE'、テーブル= NONE''ので、あなたは、TR '( 'table.findを行っている返します') 'と' None.find() 'は未定義です。 – Adirio

+0

id(TableWithRules)はdivタグに関連付けられており、tbodyと書かれています –

+0

また、すべての行とすべての列を取得するためにはfind_allを使用しなければなりません.find()したがって、結果セットを返しません –

答えて

2
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 

divtag = 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) 
関連する問題