May-06-2018, 08:30 AM
Hi All,
My code below is currently returning:
LNG Stock Level 2016-09-30T14:00:14Z 2016-03-14T00:00:00Z 6722.422335
Which is what I want it to do, however there should be a lot more data than there is. When I look at the XML data that the api returns there is a lot of data points however I cannot get my code to output it all.
It should be doing something like this, but its not:
LNG Stock Level 2016-09-30T14:00:14Z 2016-03-14T00:00:00Z 6722.422335
LNG Stock Level 2016-09-30T14:00:14Z 2016-03-14T00:00:00Z 3048.422335
LNG Stock Level 2016-09-30T14:00:14Z 2016-03-14T00:00:00Z 3430.422335
My code below is currently returning:
LNG Stock Level 2016-09-30T14:00:14Z 2016-03-14T00:00:00Z 6722.422335
Which is what I want it to do, however there should be a lot more data than there is. When I look at the XML data that the api returns there is a lot of data points however I cannot get my code to output it all.
It should be doing something like this, but its not:
LNG Stock Level 2016-09-30T14:00:14Z 2016-03-14T00:00:00Z 6722.422335
LNG Stock Level 2016-09-30T14:00:14Z 2016-03-14T00:00:00Z 3048.422335
LNG Stock Level 2016-09-30T14:00:14Z 2016-03-14T00:00:00Z 3430.422335
import requests from lxml import etree def getXML(): toDate = "2016-03-16" fromDate = "2016-03-14" dateType = "gasday" url="http://marketinformation.natgrid.co.uk/MIPIws-public/public/publicwebservice.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> <GetPublicationDataWM xmlns="http://www.NationalGrid.com/MIPI/"> <reqObject> <LatestFlag>Y</LatestFlag> <ApplicableForFlag>Y</ApplicableForFlag> <ToDate>%s</ToDate> <FromDate>%s</FromDate> <DateType>%s</DateType> <PublicationObjectNameList> <string>LNG Stock Level</string> </PublicationObjectNameList> </reqObject> </GetPublicationDataWM> </soap12:Body> </soap12:Envelope>""" % (toDate, fromDate,dateType) response = requests.post(url,data=body,headers=headers) return response.content root = etree.fromstring(getXML()) # map prefix 'd' to the default namespace URI ns = { 'd': 'http://www.NationalGrid.com/MIPI/'} publication_objects = root.xpath('//d:CLSMIPIPublicationObjectBE', namespaces=ns) for obj in publication_objects: name = obj.find('d:PublicationObjectName', ns).text data = obj.find('d:PublicationObjectData/d:CLSPublicationObjectDataBE', ns) applicable_at = data.find('d:ApplicableAt', ns).text applicable_for = data.find('d:ApplicableFor', ns).text value = float(data.find('d:Value', ns).text) print(name,applicable_at,applicable_for,value)