我正在尝试从该网站上名为“燃料混合图”的表中提取数据:https://www.iso-ne.com/isoexpress/ 我正在使用 BeautifulSoup 读取 HTML 并提取下面列出的表格,但是当我尝试读取 tbody 的内容时,它将其输出为空。
这是我的代码:
from bs4 import BeautifulSoup
from urllib.request import urlopen
pullPage = 'https://www.iso-ne.com/isoexpress/'
#query website and assign HTML to var page
page = urlopen(pullPage)
#parse HTML into var soup
soup = BeautifulSoup(page, 'html.parser')
#take <div> out of HTML name classifier and obtain value
fuelMix = soup.find('div', id='p_p_id_fuelmixgraphportlet_WAR_isoneportlet_INSTANCE_ZXnKx0ygssKj_')
fuelMixData = fuelMix.find('table', id = '_fuelmixgraphportlet_WAR_isoneportlet_INSTANCE_ZXnKx0ygssKj_table')
tbody = fuelMixData.find_all('tbody')
#for row in rows:
# data = row.find_all('td')
#FMData.append(str(row.find_all('tr')[0].text))
print (tbody)
这里是 HTML 的相关部分:
<table id="_fuelmixgraphportlet_WAR_isoneportlet_INSTANCE_ZXnKx0ygssKj_table" align="left">
<thead>
<tr>
<th style="text-align:left;">Date/Time</th>
<th style="text-align:left;">Fuel</th>
<th>MW</th> </tr>
</thead>
<tbody>
<tr>
<td style="text-align:left;">06/02/2019 00:01</td>
<td style="text-align:left;">NaturalGas</td>
<td>2581</td>
</tr>
<tr>
<td style="text-align:left;">06/02/2019 00:01</td>
<td style="text-align:left;">Nuclear</td>
<td>3339</td>
</tr>
</tbody>
</table>
目前,我的预期结果是简单地打印 tbody 中的所有数据。最终我将读取“tr”和“td”来创建数据数组(任何有关如何清理非日期/时间、燃料类型和值的其他字符串的想法也将受到赞赏!)
当我运行当前代码时,它只会返回
[<tbody></tbody>]
如果我 find_all('tr'),它只返回来自 thead 的值:
[<tr> <th style="text-align:left;">Date/Time</th> <th style="text-align:left;">Fuel</th> <th>MW</th> </tr>]
如果我 find_all('td'),则会返回一个空数组。
感谢您提前提供的帮助。
最佳答案
模仿页面执行的 POST 请求,您将获得 json 格式的所有信息
from bs4 import BeautifulSoup as bs
import requests
import time
params = {
'_nstmp_formDate' : int(time.time()),
'_nstmp_startDate' : '06/02/2019',
'_nstmp_endDate' : '06/02/2019',
'_nstmp_twodays' : 'false',
'_nstmp_chartTitle' : 'Fuel Mix Graph',
'_nstmp_requestType' : 'genfuelmix',
'_nstmp_fuelType' : 'all',
'_nstmp_height' : 250,
'_nstmp_showtwodays' : 'false'
}
r = requests.post('https://www.iso-ne.com/ws/wsclient', data = params).json()
例如写入 df:
from bs4 import BeautifulSoup as bs
import requests
import time
import pandas as pd
params = {
'_nstmp_formDate' : int(time.time()),
'_nstmp_startDate' : '06/02/2019',
'_nstmp_endDate' : '06/02/2019',
'_nstmp_twodays' : 'false',
'_nstmp_chartTitle' : 'Fuel Mix Graph',
'_nstmp_requestType' : 'genfuelmix',
'_nstmp_fuelType' : 'all',
'_nstmp_height' : 250,
'_nstmp_showtwodays' : 'false'
}
r = requests.post('https://www.iso-ne.com/ws/wsclient', data = params).json()
result = []
headers = ['NaturalGas', 'Wind', 'Nuclear', 'Solar', 'Wood', 'Refuse', 'LandfillGas', 'BeginDateMs', 'Renewables', 'BeginDate', 'Hydro', 'Other']
for item in r[0]['data']:
row = {}
for header in headers:
row[header] = item.get(header, '')
result.append(row)
df = pd.DataFrame(result, columns = headers)
print(df.head())
关于python - 抓取表仅返回 "tbody"而不是 tbody 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56419299/