Python BeautifulSoup 代码示例:解析 Android API 文档并生成 JSON 文件
import requests import json
from bs4 import BeautifulSoup
def parse_node(node): ''' 递归解析 HTML 节点,提取其属性和文本内容
Args:
node: BeautifulSoup 节点对象
Returns:
dict: 解析结果,包含节点的属性和文本内容
'''
result = {}
if node.has_attr('id'):
result['id'] = node['id']
if node.has_attr('class'):
result['class'] = node['class']
if node.name == 'a':
result['href'] = node['href']
result['text'] = node.text.strip()
elif node.name in ('span', 'p'):
result['text'] = node.text.strip()
else: # 如果节点既不是a标签,也不是span或p标签,则需要遍历其所有子节点
for child in node.children:
child_result = parse_node(child) # 递归调用parse_node函数,解析子节点
if child_result:
result.setdefault(child.name, []).append(child_result) # 将子节点解析结果添加到当前节点的子节点列表中
return result
url = 'https://developer.android.google.cn/reference/kotlin/android/app/Application'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser') root = soup.find('div', {'id': 'android.app.Application'})
data = parse_node(root)
with open('android_app.json', 'w', encoding='utf-8') as f: # 添加文件编码方式 json.dump(data, f, indent=4, ensure_ascii=False) # 添加ensure_ascii参数,确保输出中文字符不被转义,使其可读性更强
原文地址: https://www.cveoy.top/t/topic/loM9 著作权归作者所有。请勿转载和采集!