1

我正在尝试使用名为“lead-value”的类访问 div 中的值。这是第二次出现带有值为“lead-value”的类的 div,因此我试图通过在第二个“lead-value”出现的父项下设置子集来访问此特定实例。

这是html:

<td title="College Readiness is based on the percentages of 12th graders who were tested and passed AP&#174; exams. The maximum college readiness index value is 100.0." class="column-last column-even table-column-last table-column-even  g_school_in_country_college_readiness_index_stacked  cluetip">

                    <div>
    <p><div class="lead-value">100.0</div>

所以我想使用怪物类名"column-last column-even table-column-last table-column-even g_school_in_country_college_readiness_index_stacked cluetip"来获取值'100'。

我将如何使用 BeautifulSoup 做到这一点?

4

1 回答 1

3

例如,原始的 example.html 文件是这样的:

<div class="lead-value">80.0</div>
<div class="lead-value">100.0</div>
<div class="lead-value">120.0</div>
<div class="lead-value">140.0</div>

蟒蛇代码是:

>>>inf = open("example.html") 
>>>content = inf.read()
>>>inf.close()
>>>soup = BeautifulSoup(content)
>>>soup.findall('div',{'class':'lead-value'})
[<div class="lead-value">80.0</div>, <div class="lead-value">100.0</div>, <div class="lead-value">120.0</div>, <div class="lead-value">140.0</div>]
>>>blocks = soup.findall('div',{'class':'lead-value'}) 
>>>print blocks[1].string
100.0
于 2013-07-29T14:43:15.180 回答