Python BeautifulSoup:从div标签检索文本 [英] Python BeautifulSoup: Retrieving text from div tag

查看:118
本文介绍了Python BeautifulSoup:从div标签检索文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是网络剪贴中的新手.我正在使用漂亮的汤来提取Google Play商店.但是,我坚持从div标签检索文本. Div标签如下所示:

I am newbie in web scrapping. I am using beautiful soup for extracting google play store. However, I stuck to retrieve text from a div tag. Div tag looks like below:

a = <`div class="LVQB0b"><div class="QoPmEb"></div><div><span class="X43Kjb">Education.com</span><span class="p2TkOb">August 15, 2019</span></div>Thanks for your feedback. We are sorry to hear you're having trouble with the app. This is a known issue and our team has fixed it. Please restart the app and let us know at support@education.com if you have any further trouble. Thanks!</div>` 

我想从感谢您的反馈"开始检索文本.我使用以下代码来检索文本:

I want to retrieve the text starting from "Thanks for your feedback". I used the following code to retrieve the text:

response = a.find('div',{'class':'LVQB0b'}).get_text()

但是,上述命令还会返回不需要的文本,即"Education.com"和日期.我不确定如何从不具有类名的div标签中检索文本,如上面的示例所示.等待您的指导.

However, the above command also returns unwanted text i.e. 'Education.com' and the date. I am not sure how to retrieve the text from div tag which does not have class name as shown above in the example. Waiting for your guidance.

推荐答案

使用find(text=True, recursive=False)

例如:

from bs4 import BeautifulSoup

s = '''<div class="LVQB0b"><div class="QoPmEb"></div><div><span class="X43Kjb">Education.com</span><span class="p2TkOb">August 15, 2019</span></div>Thanks for your feedback. We are sorry to hear you're having trouble with the app. This is a known issue and our team has fixed it. Please restart the app and let us know at support@education.com if you have any further trouble. Thanks!</div>'''    
html = BeautifulSoup(s, 'html.parser')
print(html.find('div',{'class':'LVQB0b'}).find(text=True, recursive=False))

输出:

Thanks for your feedback. We are sorry to hear you're having trouble with the app. This is a known issue and our team has fixed it. Please restart the app and let us know at support@education.com if you have any further trouble. Thanks!

这篇关于Python BeautifulSoup:从div标签检索文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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