在HTML中的标题后打印“p”标记的内容

问题描述 投票:0回答:2

我正在尝试完成数据刮刀分配。这一切都有效,除了最后一部分,我需要根据用户搜索标准打印报告给网站的网络流量漏洞的描述。

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>...

我想要“描述”标题的内容(在p元素中)(这是一个h3元素)。我已经尝试了类似的“find_next_sibling”,但似乎无法使其正常工作。

任何建议表示赞赏。

python html scraper
2个回答
1
投票

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

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

0
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.