我必须通过网站上的搜索查询搜索CVE或常见漏洞和曝光,然后在我的打印请求中打印导入的结果表。我用来抓取结果的网站是https://cve.mitre.org/cgi-bin/cvekey.cgi?keyword=CVE+2017
我正在使用python 3进行编码并且是新手,并希望将其用作项目
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 :(得分: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
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)
答案 1 :(得分:1)
“正确”的答案不正确
这行是错误的:
divtag=soup.find('div',{'id':'TableWithRules'})
应该是:
table=soup.find('div',{'id':'TableWithRules'})