BeautifulSoup获取字符串为None

网页源文件

            <tbody>

                  <tr>

                    <td>1td>
                    <td class="align-left">
                    <a href="World-University-Rankings/Harvard-University.html" target="_blank">哈佛大学a>

                    td>
                    <td><a href="./World-University-Rankings-2016/USA.html" target="_blank"><img src="./houtai/templates/flag/USA.png">a>td>

                    <td class="hidden-xs">1td>

                    <td>100.0td>
                    <td class="hidden-xs need-hidden alumni">100.0td>

                        <td class="hidden-xs need-hidden award" style="display:none;">100.0td>

                    <td class="hidden-xs need-hidden hici" style="display:none;">100.0td>
                    <td class="hidden-xs need-hidden ns" style="display:none;">100.0td>
                    <td class="hidden-xs need-hidden pub" style="display:none;">100.0td>
                    <td class="hidden-xs need-hidden pcp" style="display:none;">79.2td>
                tr>
            tobody>

如下代码:

def fillUnivList(ulist, html):
    soup = BeautifulSoup(html, "html.parser")
    for tr in soup.find('tbody').children:
        if isinstance(tr, bs4.element.Tag):
            tds = tr('td')
            ulist.append([tds[0].string, tds[1].string, tds[5].string])

出错结果:
BeautifulSoup获取字符串为None_第1张图片

原因:

用 .string 属性来提取标签里的内容时,该标签应该是只有单个节点的。比如上面的 1 标签那样。

修改代码如下:

def fillUnivList(ulist, html):
    soup = BeautifulSoup(html, "html.parser")
    for tr in soup.find('tbody').children:
        if isinstance(tr, bs4.element.Tag):
            tds = tr('td')
            astr = tds[1].find('a')
            ulist.append([tds[0].string, astr.string, tds[5].string])

正确结果:
BeautifulSoup获取字符串为None_第2张图片

你可能感兴趣的:(python)