且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用BeautifulSoup导航到第二个字符串文本

更新时间:2023-02-06 19:10:46

您可以使用 CSS选择器以获取名称和数字:

You can use CSS selectors to get names and numbers:

names = soup.select('ul#secondaryconsumers > li.secondaryconsumerlist > div.name')
numbers = soup.select('ul#secondaryconsumers > li.secondaryconsumerlist > div.number')

print [name.text for name in names]
print [number.text for number in numbers]

打印:

[u'A3', u'B3']
[u'100', u'98']


注释中后续问题的示例代码:


Example code for the follow-up question in comments:

from bs4 import BeautifulSoup


data = """
<div class="span9">
    <table class="result-data table" border="0">
        <tbody>
        <tr class="result-item highlighting">
            <td class="result-category" scope="row">Name:</td>
            <td class="result-value-bold" colspan="4" itemprop="item">
                Robin Hood
            </td>
        </tr>
        </tbody>
    </table>
</div>
"""

soup = BeautifulSoup(data)
print soup.find('td', class_="result-value-bold").get_text(strip=True)

打印Robin Hood.

或者,或者首先找到父tabletr:

Or, alternatively first find parent table and tr:

table = soup.find('table', class_='result-data')
tr = table.find('tr', class_='result-item')
print tr.find('td', class_="result-value-bold").get_text(strip=True)