我试图从一个html文件中提取文本。
html
文件如下所示:
<li class="toclevel-1 tocsection-1">
<a href="#Baden-Württemberg"><span class="tocnumber">1</span>
<span class="toctext">Baden-Württemberg</span>
</a>
</li>
<li class="toclevel-1 tocsection-2">
<a href="#Bayern">
<span class="tocnumber">2</span>
<span class="toctext">Bayern</span>
</a>
</li>
<li class="toclevel-1 tocsection-3">
<a href="#Berlin">
<span class="tocnumber">3</span>
<span class="toctext">Berlin</span>
</a>
</li>
我想从最后一个span
标记中提取最后一个文本。
第一行是“巴登-W”ürtemberg“在class="toctext"
之后,然后将其放入python列表
在Python中,我尝试了以下方法:
names = soup.find_all("span",{"class":"toctext"})
我的输出是list
:
[<span class="toctext">Baden-Württemberg</span>, <span class="toctext">Bayern</span>, <span class="toctext">Berlin</span>]
那么我怎样才能只提取标签之间的文本呢
谢谢大家
有了一份理解列表,你可以做到以下几点:
find_all
方法返回一个列表。遍历列表以获取文本退货:
内置的python
dir()
和type()
方法总是便于检查对象相关问题 更多 >
编程相关推荐