我尝试在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)
答案 0 :(得分:1)
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>]
>>>