使用Beautiful Soup从表中刮取值

时间:2018-06-14 20:39:37

标签: python beautifulsoup

我试图抓取一些历史天气数据,并且无法弄清楚如何从表中提取值。我已经能够在表格中打印行,但是当我尝试提取“#td”时。 (更具体地说,值)来自每一行,我得到一个属性错误。这就是我到目前为止所做的:

import requests
from random import choice
from bs4 import BeautifulSoup
import pandas as pd

#---------------------------------------------------------------------------------------#
url = "https://www.wunderground.com/history/airport/KORD/2017/4/1/CustomHistory.html?dayend=10&monthend=4&yearend=2017&req_city=&req_state=&req_statename=&reqdb.zip=&reqdb.magic=&reqdb.wmo="
page = requests.get(url)

soup = BeautifulSoup(page.text,"lxml")
#---------------------------------------------------------------------------------------#
table = soup.find('table', id='obsTable')

table_head = table.find('thead')
header_1 = []    
for th in table_head.find_all('th'):
    key_1 = th.get_text()
    header_1.append(key_1)
#---------------------------------------------------------------------------------------#
table_head_2 = table.find_all('tr')[1]
header_2 = []
for td in table_head_2.find_all('td'):
    key_2 = td.get_text()
    header_2.append(key_2)
#---------------------------------------------------------------------------------------#    
rows = table.find_all('tr')[2]

for row in rows.find_all('td'):
    print(row)

当我打印一行数据时会返回此信息:

<tr>
<td><a href="/history/airport/KORD/2017/4/1/DailyHistory.html">1</a></td>
<td>
<span class="wx-value">59</span>
</td>
<td>
<span class="wx-value">47</span>
</td>
<td>
<span class="wx-value">34</span>
</td>
<td>
<span class="wx-value">31</span>
</td>
<td>
<span class="wx-value">23</span>
</td>
<td>
<span class="wx-value">16</span>
</td>
<td>
<span class="wx-value">82</span>
</td>
<td>
<span class="wx-value">51</span>
</td>
<td>
<span class="wx-value">20</span>
</td>
<td>
<span class="wx-value">30.24</span>
</td>
<td>
<span class="wx-value">30.19</span>
</td>
<td>
<span class="wx-value">30.09</span>
</td>
<td>
<span class="wx-value">10</span>
</td>
<td>
<span class="wx-value">10</span>
</td>
<td>
<span class="wx-value">10</span>
</td>
<td>
<span class="wx-value">13</span>
</td>
<td>
<span class="wx-value">6</span>
</td>
<td>
<span class="wx-value">17</span>
</td>
<td>
<span class="wx-value">0.00</span>
</td>
<td>
     
</td>
</tr>

如果有人能帮助我,我会很感激。

1 个答案:

答案 0 :(得分:1)

如果您只是尝试打印值,可以通过调用.text的{​​{1}}属性来完成此操作。但是,值周围有很多空格,因此您需要row它们。

strip()

将返回:

for row in rows.find_all('td'):
    print(row.text.strip())