刮除表仅返回“ tbody”,而不返回tbody的内容

时间:2019-06-02 22:10:31

标签: python html web-scraping

我正在尝试从以下站点的“燃料混合图”表中提取数据: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'来创建数据数组(关于如何清除日期,时间,燃料类型和值以外的其他字符串的任何想法,也将不胜感激!)< / p>

运行当前代码时,它只会返回

[<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'),则返回一个空数组。

谢谢您的帮助。

1 个答案:

答案 0 :(得分:1)

模仿页面执行的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())