繁体   English   中英

在HTML标头后打印“ p”标签的内容

[英]print the content of the “p” tag after a header in HTML

我正在尝试完成数据收集器分配。 除最后一部分外,所有其他工作均有效,在该部分中,我需要根据用户搜索条件打印报告给网站的网络安全漏洞的描述。

for index in range(2): 
    response = requests.get(url_values[index])
    content = response.content
    soup = BeautifulSoup(content,"lxml")
    #find the table content
    for header in soup.find_all("h3", string = "Description"):
        text = find_next.("p")
        print (text)

这就是我要从中获取信息的区域中的HTML外观:

 ...<section class="content-band">              
        <div class="content">



            <h3>Risk</h3>                           

            <div><p>Low</p></div>






            <h3>Date Discovered</h3>
            <p>February 12, 2019</p>




            <h3>Description</h3>
            <p>Microsoft Windows is prone to a local information-disclosure 
             vulnerability.                                                                        

            Local attackers can exploit this issue to obtain sensitive 
            information that may lead to further attacks.</p>




            <h3>Technologies Affected</h3>...

我想要“ Description”标头(这是一个h3元素)的内容(在ap元素中)。 我已经尝试了类似的“ find_next_sibling”,似乎无法正常工作。

任何建议表示赞赏。

您可以从h3兄弟元素中获取文本,如下所示:

print(soup.find("h3", string="Description").find_next_sibling().text)

您可以在同一个汤对象上使用两个.find()方法来找到“ h3”元素,然后在该对象下找到“ p”元素。

text = soup.find("h3", string="Description").find("p").text

您不需要使用.find_all()因为只有一个“ h3”元素带有文本“ Description”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM