python - 使用 beautifulsoup 获取 div 中的 child 文本

标签 python html python-2.7 beautifulsoup urllib2

您好,我想了解 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"})

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

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

谁能帮帮我?

最佳答案

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

for res in result:
    print(res.text)

.text是代理 Element.get_text() method 的属性.

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

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

关于python - 使用 beautifulsoup 获取 div 中的 child 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20889790/

相关文章:

python - 计算范围内所有 n 个数的四面体编号,将其存储在列表中并返回

python - 在 Python 中附加到嵌套列表或字典

python 到 MATLAB 代码、数字列表和求和

python - 第一个字符被删除(正则表达式)

python - 为什么 python-3.x 删除了 ROT-13 作为编码?

html - IE中如何让主div出现在中心?

html - Bootstrap CSS/HTML 背景图像调整大小

html - Bootstrap 中行内的多列在 IE 11 中不起作用

python - 用 Python 抓取网站的第二页不起作用

python-2.7 - 移动应用聊天解决方案