我无法将 XML 获取到 python 数据框
您能帮我将 XML 解析为 python 数据框吗?我似乎无法让它工作这是我走了多远:
import xmltodict
import pandas as pd
import requests
from bs4 import BeautifulSoup
def get_xml():
url="http://energywatch.natgrid.co.uk/EDP-PublicUI/PublicPI/InstantaneousFlowWebService.asmx"
headers = {'content-type': 'application/soap+xml; charset=utf-8'}
body ="""<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<GetInstantaneousFlowData xmlns="http://www.NationalGrid.com/EDP/UI/" />
</soap12:Body>
</soap12:Envelope>"""
response = requests.post(url,data=body,headers=headers)
return response.content
response = get_xml()
soup = BeautifulSoup(response, 'lxml')
table_columns = []
for item in soup.find_all(['EDPObjectName'.lower()]):
table_columns.append(item.text)
table_columns=pd.DataFrame(table_columns)
table_rows=[]
for item in soup.find_all(['applicableat']):
table_rows.append(item.text)
df1=pd.DataFrame(table_rows).drop_duplicates()
#df1=pd.to_datetime(df1)
table=[]
for item in soup.find_all(['flowrate']):
table.append(item.text)
df=pd.DataFrame(table)
df_final=pd.DataFrame(df, columns=table_columns, index=df1)
这是我正在寻找的结果:
ALDBROUGH AVONMOUTH BACTON BBL …
2019-08-08T13:00:00 0 1.23 5.1 …
2019-08-08T13:02:00 0 1.23 5.1 …
2019-08-08T13:04:00 0 3.23 5.1 …
2019-08-08T13:06:00 0 3.23 5.1 …
2019-08-08T13:08:00 0 3.23 5.23 …
2019-08-08T13:10:00 0 4.23 5.204 …