66M*_*Mhz 3 python beautifulsoup
我正在尝试从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>]
Run Code Online (Sandbox Code Playgroud)
我想提取存储在最后一个div标签中的文本,但我不确定如何迭代 post_content
Pad*_*ham 27
html = """
<div class="body_content_inner">
post #1 content is here
</div>, <div class="body_content_inner">
post #2 content is here
</div>
"""
soup = BeautifulSoup(html)
print soup.find_all("div")[-1].get_text()
post #2 content is here
Run Code Online (Sandbox Code Playgroud)
last_div = None
for last_div in post_content:pass
if last_div:
content = last_div.getText()
Run Code Online (Sandbox Code Playgroud)
然后你得到post_content的最后一项.