使用BeautifulSoup find_all从最后一个元素获取内容 [英] Getting content from last element using BeautifulSoup find_all

查看:54
本文介绍了使用BeautifulSoup find_all从最后一个元素获取内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从find_all创建的列表中的最后一个div中提取内容.

I'm trying to extract the content from the last div in in a list created by find_all.

post_content = soup.find_all('div',{'class':'body_content_inner'})

存储以下文本:

[<div class="body_content_inner">
 post #1 content is here
 </div>, <div class="body_content_inner">
 post #2 content is here
 </div>]

我想提取存储在最后一个div标签中的文本,但是我不确定如何遍历 post_content

I'd like to extract the text that is stored within the last div tag but I am unsure how to iterate through post_content

推荐答案

last_div = None
for last_div in post_content:pass
if last_div:
    content = last_div.getText()

然后您获得post_content的最后一项.

And then you get the last item of post_content.

这篇关于使用BeautifulSoup find_all从最后一个元素获取内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆