加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

使用BeautifulSoup find_all从最后一个元素获取内容

发布时间:2020-12-20 13:05:07 所属栏目:Python 来源:网络整理
导读:我正在尝试从find_all创建的列表中的最后一个div中提取内容. 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" p
我正在尝试从find_all创建的列表中的最后一个div中提取内容.

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

解决方法

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

然后你得到post_content的最后一项.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读