gpt4 book ai didi

python - 如何从 xml tspan 标签中检索信息

转载 作者:太空宇宙 更新时间:2023-11-04 04:04:31 25 4
gpt4 key购买 nike

我目前正在使用 BeautifulSoup 进行 xml 解析,但不知道如何从 tspan 标签中获取信息。我正在解析的 xml 如下所示:

<text
transform="matrix(0,-1,-1,0,7931,3626)"
style="font-variant:normal;font-weight:normal;font-size:92.3259964px;font-family:Arial;-inkscape-font-specification:ArialMT;writing-mode:lr-tb;fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:none"
id="text60264"><tspan
x="0 61.581444 123.16289 184.74432 251.4037 323.23334 384.81476 410.48138 436.14801 497.72946 559.31091 625.97028 692.62964 754.21112 805.54437 831.211 918.3667 979.94818 1005.6148 1077.4445 1144.1038 1200.515 1226.1816 1256.9261 1318.5076 1390.3373 1421.0818 1446.7484 1472.415 1526.3334 1587.9149 1649.4963 1716.1556 1782.8151 1844.3965 1895.7297 1921.3964 2008.5521 2070.1335 2095.8003 2167.6299 2234.2893 2290.7004"
y="0"
sodipodi:role="line"
id="tspan60262">APPROX. BARREL WEIGHT (KG): &lt;BARREL WEIGHT&gt;</tspan></text>

我可以从文本标签中获取文本,但我正在尝试获取 x="0 61.581..." 以便我可以将其更改为 x="0”。到目前为止,我的代码只获取了 tspan xml 标签

from bs4 import BeautifulSoup


infile = open('1c E37 Face Electric Operation No No.svg', 'r')
contents = infile.read()
soup = BeautifulSoup(contents, 'lxml')
items = soup.find_all('tspan')
for item in items:
print(item)

最佳答案

您也可以使用 css 选择器 - 通过将 x 属性添加到选择器,您将只会获得存在该属性的元素

from bs4 import BeautifulSoup as bs

s = '''
<text
transform="matrix(0,-1,-1,0,7931,3626)"
style="font-variant:normal;font-weight:normal;font-size:92.3259964px;font-family:Arial;-inkscape-font-specification:ArialMT;writing-mode:lr-tb;fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:none"
id="text60264"><tspan
x="0 61.581444 123.16289 184.74432 251.4037 323.23334 384.81476 410.48138 436.14801 497.72946 559.31091 625.97028 692.62964 754.21112 805.54437 831.211 918.3667 979.94818 1005.6148 1077.4445 1144.1038 1200.515 1226.1816 1256.9261 1318.5076 1390.3373 1421.0818 1446.7484 1472.415 1526.3334 1587.9149 1649.4963 1716.1556 1782.8151 1844.3965 1895.7297 1921.3964 2008.5521 2070.1335 2095.8003 2167.6299 2234.2893 2290.7004"
y="0"
sodipodi:role="line"
id="tspan60262">APPROX. BARREL WEIGHT (KG): &lt;BARREL WEIGHT&gt;</tspan></text>
'''

soup = bs(s, 'lxml')
print([i['x'] for i in soup.select('tspan[x]')])

关于python - 如何从 xml tspan 标签中检索信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57598550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com