【发布时间】:2018-10-01 03:26:45
【问题描述】:
我想尝试使用漂亮的汤和请求从该网站http://wiki.stat.ucla.edu/socr/index.php/SOCR_Data_Dinov_020108_HeightsWeights 获取数据。这是我的代码:
import requests
from bs4 import BeautifulSoup
response = requests.get("http://wiki.stat.ucla.edu/socr/index.php/SOCR_Data_Dinov_020108_HeightsWeights")
soup = BeautifulSoup(response.text, "html.parser")
list_table_data = soup.find(class_="wikitable").contents
list_tr_data = list_table_data[1::2]
print(list_tr_data)
当您打印 list_tr_data 时,输出变为:
[<tr>
<th>Index</th><th>Height(Inches)</th><th>Weight(Pounds)
</th></tr>, <tr>
<td>1</td><td>65.78</td><td>112.99
</td></tr>, <tr>
<td>2</td><td>71.52</td><td>136.49
</td></tr>, <tr>
<td>3</td><td>69.40</td><td>153.03
</td></tr>,...., <tr>
<td>200</td><td>71.39</td><td>127.88
</td></tr>]
我希望将此高度(英寸)数据放入名为 list_height_data 的列表中,但是当我尝试使用此代码进行访问时:
list_height_data = []
for row in list_tr_data:
list_height_data.append(row.find_all("tr"))
print(list_height_data)
这会导致一个空列表:
[[], [], [], [], [], [], [], [], [], [], ... []]
我应该怎么做才能获取高度(英寸)数据?如果你打印list_height_data和打印len(list_height_data)应该变成:
[65.78, 71.52, 69.40, ..., 71.39]
200
【问题讨论】:
标签: python web-scraping beautifulsoup web-crawler