美丽的汤没有回报 [英] Beautiful soup returns None

查看:56
本文介绍了美丽的汤没有回报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下html代码,我用漂亮的汤来提取信息.例如,我想获取关系状态:关系

I have the following html code and i use beautiful soup to extract information. I want to get for example Relationship status: Relationship

<table class="box-content-list" cellspacing="0">
            <tbody>
             <tr class="first">
              <td>
                   <strong>
                    Relationship status:
                   </strong>
               Relationship
              </td>
             </tr>
             <tr class="alt">
              <td>
               <strong>
                Living:
              </strong>
               With partner
              </td>
             </tr>

我创建了以下代码:

xs = [x for x in soup.findAll('table', attrs = {'class':'box-content-list'})]       
    for x in xs:
        #print x
        sx = [s for s in x.findAll('tr',attrs={'class':'first'})]
        for s in sx:
            td_tabs = [td for td in s.findAll('td')]
            for td in td_tabs:
                title = td.findNext('strong')
                #print str(td)
                status = td.findNextSibling()
                print title.string
                print status

但是我得到的结果是关系"状态:并且打印状态为无打印".我在做什么错了?

but the result i get is Relations status: and the print status is printing None. What i am doing wrong?

推荐答案

有一种特殊的方法 get_text (或旧的BeautifulSoup版本中的 getText )来获取内容的内容.复杂的标签.以您的示例为例:

There is a special method get_text (or getText in old BeautifulSoup versions) to get the content of intricated tags. With your example:

>>> example.td.get_text(' ', strip=True)
'Relationship status: Relationship'

第一个参数是要使用的分隔符.

The first parameter is the separator to use.

这篇关于美丽的汤没有回报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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