尝试使用Python和BeautifulSoup解析网页以获取最新的高级漏洞

时间:2018-06-08 13:09:48

标签: python beautifulsoup python-requests lxml

我试图将其他人的建议应用于此处:

  

Beautiful Soup: Accessing <li> elements from <ul> with no id

但我无法让它发挥作用。看来这个问题的人有一个 'parent'h2标题,但我试图解析的那个没有。

这是我正在抓的网页:

  

https://nvd.nist.gov/

(我认为)我找到了我需要操作的元素,它是<ul id="latestVulns">及其后续的li部分。

我基本上想要查找“最近20个评分的漏洞ID和摘要”部分,并根据漏洞的不同,发送电子邮件到我工作地点的相应部门。

到目前为止,这是我的代码:

from bs4 import BeautifulSoup
import requests

source = requests.get('https://nvd.nist.gov/')
soup = BeautifulSoup(source.content, 'lxml')

section = soup.find('latestVulns')
print(section)

此代码返回None

我不知所措

1 个答案:

答案 0 :(得分:1)

find的第一个参数需要元素的名称,并且您正在传递id

您可以使用它来正确找到标签

section = soup.find('ul', {'id': 'latestVulns'})