You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用BeautifulSoup获取DocumentIndex标签的纯文本值

提取BeautifulSoup中DocumentIndex标签的数值列表

你可以通过提取每个标签的文本内容并转换为数值,得到期望的纯数值列表,修改后的代码如下:

from bs4 import BeautifulSoup

lstr_soup = BeautifulSoup(gstr_xml, features="xml")
# 提取所有DocumentIndex标签的文本并转为整数
lstr_folder_index = [int(tag.text) for tag in lstr_soup.findAll('DocumentIndex')]
print(lstr_folder_index)

关键说明:

  • tag.text(或tag.get_text())用于获取标签包裹的纯文本内容,比如从<DocumentIndex>3646</DocumentIndex>中提取出"3646"
  • 使用int()将文本字符串转换为整数,若需要保留字符串格式,去掉int()即可:
    lstr_folder_index = [tag.text for tag in lstr_soup.findAll('DocumentIndex')]
    

内容的提问来源于stack exchange,提问作者donny

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 00:25:28