使用beautifulsoup在div中获取孩子的文本 [英] Get text of children in a div with beautifulsoup

查看:23
本文介绍了使用beautifulsoup在div中获取孩子的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想了解 Google Play 商店中某个应用的说明.(https://play.google.com/store/apps/details?id=com.wetter.androidclient&hl=de)

导入 urllib2从 bs4 导入 BeautifulSoup汤 = 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:..:"

有人可以帮我吗?

解决方案

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

 for res in result:打印资源文本

或者,如果只有一个这样的

,使用.find()而不是>.find_all():

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

Hi i want the description of an App in the Google Playstore. (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"})

With this code i get the whole content in this class. But i can't get only the text in it. I tried a lot of things with next_silbing or .text but it always throws errors(ResultSet has no attribute xxx).

I just want to get the text like this: "Die Android App von wetter.com! Sie erhalten: ..:"

Anyone can help me?

解决方案

Use the .text attribute on the elements; you have a list of results, so loop:

for res in result:
    print res.text

Alternatively, if there is only ever supposed to be one such <div>, use .find() instead of .find_all():

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

这篇关于使用beautifulsoup在div中获取孩子的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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