如何用Beautiful Soup提取XML中内嵌于标签的职业等特征值
解决思路
你遇到的问题是不同字段的存储位置不一致:gender、given这类字段的值存在interp标签的value属性中,而occupation字段的值存在rs标签的内部文本中,现有代码统一拿value属性自然取不到职业值,调整逻辑适配两种存储规则即可。
修改后的代码
你可以在最后取值时增加兼容逻辑:优先取标签的value属性,属性不存在时自动取标签内部的文本内容,同时可以调整职业标签的匹配规则适配XML里的join关联逻辑,完整修改如下:
case_feature_keys = ['year', 'offenceCategory', 'offenceSubcategory'] person_feature_keys = ['gender', 'age', 'occupation', 'given'] outcome_key = 'verdictCategory' def get_tag_value(tag): # 通用取值函数:优先取value属性,没有则取内部文本,自动去首尾空白 if not tag: return None val = tag.get("value") if val is not None: return val return tag.get_text(strip=True) def get_person_features(trial_account, person_type: str): person_features = {} # 先收集所有人物和职业的关联关系 occu_joins = trial_account.find_all("join", result="persNameOccupation") occu_map = {} for join in occu_joins: person_id, occu_id = join.get("targets", "").split() occu_map[occu_id] = person_id for key in person_feature_keys: matches = [] all_tags = trial_account.find_all(type=key) if key != "occupation": # 普通属性:在persName下,匹配人物类型 matches = [x for x in all_tags if person_type in x.parent.attrs.get("type", "")] else: # 职业属性:通过join关联匹配对应人物类型 for tag in all_tags: tag_id = tag.get("id") linked_person_id = occu_map.get(tag_id) if linked_person_id: person_tag = trial_account.find(id=linked_person_id) if person_tag and person_type in person_tag.attrs.get("type", ""): matches.append(tag) if matches: person_features[person_type + "_" + key] = matches[0] return person_features def process_trial_account(trial_account) -> dict: """ Takes in a single account and returns a dictionary representing a row of the table. """ case_features = {key: trial_account.find(type=key) for key in case_feature_keys} defendant_features = get_person_features(trial_account, 'defendant') victim_features = get_person_features(trial_account, 'victim') outcome = trial_account.find(type=outcome_key) features = {**case_features, **defendant_features, **victim_features, "outcome": outcome or {}} # 用通用函数取值 return {key: get_tag_value(value) for key, value in features.items()}
调整说明
- 新增了
get_tag_value通用取值函数,自动适配属性存储和文本存储两种格式,不需要单独给职业字段写特殊判断 - 补充了职业字段的关联匹配逻辑,通过XML里的
join标签匹配职业对应的人物身份,避免原来直接判断父节点属性匹配不到职业标签的问题
内容的提问来源于stack exchange,提问作者Yaz
相关产品推荐
相关产品推荐

