使用 beautifulsoup 获取 div 中的儿童文本

新手上路,请多包涵

您好,我想要 Google Play 商店中应用程序的描述。 ( https://play.google.com/store/apps/details?id=com.wetter.androidclient&hl=de )

 import urllib2
from bs4 import BeautifulSoup

soup = BeautifulSoup(urllib2.urlopen("https://play.google.com/store/apps/details?id=com.wetter.androidclient&hl=de"))
result = soup.find_all("div", {"class":"show-more-content text-body"})

使用此代码,我获得了此类中的全部内容。但我不能只得到其中的文字。我用 next_silbing 或 .text 尝试了很多东西,但它总是抛出错误(ResultSet 没有属性 xxx)。

我只想得到这样的文字:“Die Android App von wetter.com!Sie erhalten:..:”

任何人都可以帮助我吗?

原文由 Si Mon 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 466
1 个回答

在元素上使用 .text 属性;你有一个结果 _列表_,所以循环:

 for res in result:
    print(res.text)

.text 是代表 Element.get_text() 方法 的属性。

或者,如果只有 一个 这样的 <div> ,请使用 .find() 而不是 .find_all()

 result = soup.find("div", {"class":"show-more-content text-body"})
print(result.text)

原文由 Martijn Pieters 发布,翻译遵循 CC BY-SA 4.0 许可协议

推荐问题