gpt4 book ai didi

python - Beautiful Soup 找不到标签

转载 作者:太空宇宙 更新时间:2023-11-04 02:24:01 30 4
gpt4 key购买 nike

我尝试在 html 文件中搜索不同的标签(及其内容)(见下文)。我能找到的唯一标签是标签 (td3)。我为下面的 html 代码尝试的所有其他标签都会导致空结果。 html 字符串取自包含更多数据的 html 文件,但它的行为方式与原始 html 文件相同。我在 Windows 操作系统上使用 python 3.6 (anaconda)。我很乐意就该问题提出意见。

from bs4 import BeautifulSoup

string="""<?xml version="1.0" encoding="ISO-8859-1"?>
<SRPI>
<Measurement>
<Table1 corrected="no"><!--Spectrum of the sample measurement-->
<NumberOfBins>991</NumberOfBins>
<SampleFreq units="Hz">5.0000000000000000e+006</SampleFreq>
<SpecBins units="A m^2">
<data>
<complex real="-1.2725117264933506e-006" imag="-2.3275671788139798e-007"/>
<complex real="6.8157835062885686e-013" imag="4.5930196651468919e-013"/>
<complex real="-6.4585657824646980e-012" imag="4.7539847709910694e-012"/>
<complex real="-9.4822601663528955e-013" imag="3.6107393400439346e-012"/>
<complex real="-6.9440051626638662e-012" imag="1.3120684697626131e-011"/>
</data></SpecBins>
<Slope units="1">-1.3228643789347363e+000</Slope>
<SpecDen units="A^2 m^4"><SpecDen300>4.0594355476938474e-013</SpecDen300>
<SpecDen1000>3.3641372153210487e-017</SpecDen1000></SpecDen>
<NoiseDen units="A^2 m^4"><NoiseDen1000>6.6288807841262445e-025
</NoiseDen1000><NoiseDen2500>2.3871617742749738e-020</NoiseDen2500>
</NoiseDen>
</Table1>
<Table1 corrected="yes"><!--Spectrum correction file-->
<NumberOfBins>991</NumberOfBins>
<SampleFreq units="Hz">5.0000000000000000e+006</SampleFreq><SpecBins
units="A m^2">
<data>
<complex real="-6.1985809140785431e-010" imag="-1.1337902190172509e-010"/>
<complex real="2.6627789950911842e-012" imag="1.5055359912912377e-012"/>
<complex real="-3.1088631626418299e-012" imag="8.2499406092681002e-012"/>
<complex real="-3.7649152780239330e-012" imag="1.2053978204849702e-011"/>
<complex real="5.1454481954799239e-012" imag="1.7627441490145078e-011"/>
</data></SpecBins>
<Slope units="1">-1.3228179094677259e+000</Slope><SpecDen units="A^2
m^4"><SpecDen300>4.0577224679625485e-013</SpecDen300>
<SpecDen1000>3.3638569547225415e-017</SpecDen1000></SpecDen><NoiseDen
units="A^2 m^4"><NoiseDen1000>1.1074994155863766e-024</NoiseDen1000>
<NoiseDen2500>2.3085849697684034e-003</NoiseDen2500></NoiseDen>
</Table1>
<Table2><TARawDataSize>1980</TARawDataSize>
<TARawData>
<data>1.0392482964229389e-001</data>
<data>7.4384450858019771e-002</data>
<data>4.7129165401792232e-002</data>
<data>2.2414031730721266e-002</data>
<data>1.6249028219891167e-004</data>
<data>-2.0196675622799122e-002</data>
<data>-3.9955558403595014e-002</data>
</TARawData><MeasurementTime units="ms">10000</MeasurementTime>
<FieldStrength units="microTesla">1.0000000000000000e+004</FieldStrength>
<DateMeasurement>20180522_111708</DateMeasurement>
</Table2>
<Table3><SoftwareVersion> %version: 0.7 % </SoftwareVersion>
<FormatVersion>1.0</FormatVersion>
<HardwareID>VSM Spectrometer 2.1</HardwareID>
<MeasurementType>1</MeasurementType>
<CorrectionFile>C:\correct.txt</SampleID></Table3>
<Table4>
<DateCalib>20110707_091006</DateCalib>
<CalibTransmit>3.1499999999999999e+000</CalibTransmit>
<CalibReceive>
<data>
<complex real="1.3152490026529447e-005" imag="2.4057384673215519e-006"/>
<complex real="5.9217813163382713e-006" imag="7.4460089732908548e-006"/>
<complex real="2.8667597217130651e-006" imag="6.6739599860582135e-006"/>
<complex real="1.4393526301410223e-006" imag="5.6813939622965668e-006"/>
<complex real="6.6555358632601569e-007" imag="4.8684922052003432e-006"/>
<complex real="2.0061980917706046e-007" imag="4.2296081097640261e-006"/>
</data></CalibReceive><RefCoilID>Probe 1E</RefCoilID>
</Table4>
</Measurement></SRPI>"""


soup = BeautifulSoup(string,"lxml")
td1=soup.findAll('NumberOfBins')
print(td1)
td2=soup.findAll('SampleFreq')
print(td2)
td3=soup.findAll('data')
print(td3)

最佳答案

BeautifulSoup 将输入的解析树标准化并将标签转换为小写

>>> soup.findAll('numberofbins')
[<numberofbins>991</numberofbins>, <numberofbins>991</numberofbins>]
>>>
>>> soup.findAll('samplefreq')
[<samplefreq units="Hz">5.0000000000000000e+006</samplefreq>, <samplefreq units="Hz">5.0000000000000000e+006</samplefreq>]
>>>

关于python - Beautiful Soup 找不到标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50917770/

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