Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | None | 0 0
  1. GetPublicationDataWMResult, ApplicableAt, ApplicableFor, Value, ...
  2. LNG Stock Level,2016-03-13T15:00:07Z, 2016-03-12T00:00:00Z, 7050.42286, ...
  3. LNG Capacity,2016-03-13T15:00:07Z, 2016-03-12T00:00:00Z, 6515042480, ...
  4.  
  5. import requests
  6. from lxml import objectify
  7.  
  8. def getXML():
  9.  
  10. toDate = "2016-03-12"
  11. fromDate = "2016-03-12"
  12. dateType = "gasday"
  13.  
  14. url="http://marketinformation.natgrid.co.uk/MIPIws-public/public/publicwebservice.asmx"
  15. headers = {'content-type': 'application/soap+xml; charset=utf-8'}
  16.  
  17. 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">
  18. <soap12:Body>
  19. <GetPublicationDataWM xmlns="http://www.NationalGrid.com/MIPI/">
  20. <reqObject>
  21. <LatestFlag>Y</LatestFlag>
  22. <ApplicableForFlag>Y</ApplicableForFlag>
  23. <ToDate>%s</ToDate>
  24. <FromDate>%s</FromDate>
  25. <DateType>%s</DateType>
  26. <PublicationObjectNameList>
  27. <string>LNG Stock Level</string>
  28. <string>LNG, Daily Aggregated Available Capacity, D+1</string>
  29. </PublicationObjectNameList>
  30. </reqObject>
  31. </GetPublicationDataWM>
  32. </soap12:Body>
  33. </soap12:Envelope>""" % (toDate, fromDate,dateType)
  34.  
  35.  
  36. response = requests.post(url,data=body,headers=headers)
  37.  
  38. return response.content
  39.  
  40. root = objectify.fromstring(getXML())
  41.  
  42. <?xml version="1.0" encoding="utf-8"?>
  43. <soap:Envelope
  44. xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
  45. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  46. xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  47. <soap:Body>
  48. <GetPublicationDataWMResponse
  49. xmlns="http://www.NationalGrid.com/MIPI/">
  50. <GetPublicationDataWMResult>
  51. <CLSMIPIPublicationObjectBE>
  52. <PublicationObjectName>LNG Stock Level</PublicationObjectName>
  53. <PublicationObjectData>
  54. <CLSPublicationObjectDataBE>
  55. <ApplicableAt>2016-03-13T15:00:07Z</ApplicableAt>
  56. <ApplicableFor>2016-03-12T00:00:00Z</ApplicableFor>
  57. <Value>7050.42286</Value>
  58. <GeneratedTimeStamp>2016-03-13T15:56:00Z</GeneratedTimeStamp>
  59. <QualityIndicator></QualityIndicator>
  60. <Substituted>N</Substituted>
  61. <CreatedDate>2016-03-13T15:56:28Z</CreatedDate>
  62. </CLSPublicationObjectDataBE>
  63. </PublicationObjectData>
  64. </CLSMIPIPublicationObjectBE>
  65. <CLSMIPIPublicationObjectBE>
  66. <PublicationObjectName>LNG Capacity</PublicationObjectName>
  67. <PublicationObjectData>
  68. <CLSPublicationObjectDataBE>
  69. <ApplicableAt>2016-03-12T15:30:00Z</ApplicableAt>
  70. <ApplicableFor>2016-03-12T00:00:00Z</ApplicableFor>
  71. <Value>6515042480</Value>
  72. <GeneratedTimeStamp>2016-03-12T16:00:00Z</GeneratedTimeStamp>
  73. <QualityIndicator></QualityIndicator>
  74. <Substituted>N</Substituted>
  75. <CreatedDate>2016-03-12T16:00:20Z</CreatedDate>
  76. </CLSPublicationObjectDataBE>
  77. </PublicationObjectData>
  78. </CLSMIPIPublicationObjectBE>
  79. </GetPublicationDataWMResult>
  80. </GetPublicationDataWMResponse>
  81. </soap:Body>
  82. </soap:Envelope>
  83.  
  84. res= getXML()
  85.  
  86. from bs4 import BeautifulSoup
  87. soup = BeautifulSoup(res, 'html.parser')
  88.  
  89. searchTerms= ['PublicationObjectName','ApplicableAt','ApplicableFor','Value']
  90. # LNG Stock Level,2016-03-13T15:00:07Z, 2016-03-12T00:00:00Z, 7050.42286, ...
  91.  
  92. for st in searchTerms:
  93. print st+'t',
  94. print soup.find(st.lower()).contents[0]
  95.  
  96. PublicationObjectName LNG Stock Level
  97. ApplicableAt 2016-03-13T15:00:07Z
  98. ApplicableFor 2016-03-12T00:00:00Z
  99. Value 7050.42286
  100.  
  101. root = etree.fromstring(getXML())
  102.  
  103. # map prefix 'd' to the default namespace URI
  104. ns = { 'd': 'http://www.NationalGrid.com/MIPI/'}
  105.  
  106. publication_objects = root.xpath('//d:CLSMIPIPublicationObjectBE', namespaces=ns)
  107. for obj in publication_objects:
  108. name = obj.find('d:PublicationObjectName', ns).text
  109.  
  110. data = obj.find('d:PublicationObjectData/d:CLSPublicationObjectDataBE', ns)
  111. applicable_at = data.find('d:ApplicableAt', ns).text
  112. applicable_for = data.find('d:ApplicableFor', ns).text
  113. # todo: extract other relevant data and process as needed
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement