1 回答
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))
- 1 回答
- 0 关注
- 113 浏览
添加回答
举报