为了账号安全,请及时绑定邮箱和手机立即绑定

使用 beautiful-soup 提取特定标签的元素

使用 beautiful-soup 提取特定标签的元素

呼唤远方 2023-10-04 16:02:01
我想从特定标签中提取元素。例如 - 一个站点中有四个。每个标签都有其他兄弟标签,如 p、h3、h4、ul 等。我想分别查看 h2[1] 元素、h2[2] 元素。这就是我到目前为止所做的。我知道 for 循环没有任何意义。我也尝试附加文本但无法成功。然后我尝试按特定字符串进行搜索,但它给出了该特定字符串的唯一标签,而不是所有其他元素from bs4 import BeautifulSouppage = "https://www.us-cert.gov/ics/advisories/icsma-20-079-01"resp = requests.get(page)soup = BeautifulSoup(resp.content, "html5lib")content_div=soup.find('div', {"class": "content"})all_p= content_div.find_all('p')all_h2=content_div.find_all('h2')i=0for h2 in all_h2:  print(all_h2[i],'\n\n')  print(all_p[i],'\n')  i=i+1还尝试使用追加 tags = soup.find_all('div', {"class": "content"}) container = [] for tag in tags:  try:    container.append(tag.text)    print(tag.text)  except:    print(tag)我是编程方面的新手。请原谅我糟糕的编码能力。我只想看到一切都在“缓解”之下。因此,如果我想将其存储在数据库中,它将解析与一列上的缓解相关的所有内容。
查看完整描述

1 回答

?
ITMISS

TA贡献1871条经验 获得超8个赞

["p","ul","h2","div"]您可以使用findNextwith查找静态标签列表recursive=False以保持在顶层:


import requests

from bs4 import BeautifulSoup

import json


resp = requests.get("https://www.us-cert.gov/ics/advisories/icsma-20-079-01")

soup = BeautifulSoup(resp.content, "html.parser")


content_div = soup.find('div', {"class": "content"})


h2_list = [ i for i in content_div.find_all("h2")]

result = []

search_tags = ["p","ul","h2","div"]


def getChildren(tag): 

    text = []

    while (tag):

        tag = tag.findNext(search_tags, recursive=False)

        if (tag is None):

            break

        elif (tag.name == "div") or (tag.name == "h2"):

            break

        else:

            text.append(tag.text.strip())

    return "".join(text)


for i in h2_list:

    result.append({

        "name": i.text.strip(),

        "children": getChildren(i)

    })


print(json.dumps(result, indent=4, sort_keys=True))


查看完整回答
反对 回复 2023-10-04
  • 1 回答
  • 0 关注
  • 63 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信