Python Forum
Cant set api response as tree in Element tree
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cant set api response as tree in Element tree
#2
All of code is inside the function then after return nothing happens with lines 17-23.
Here a fix an i use BS with lxml as parser,i never use parser in standard library.
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')
for item in soup.find_all('EDPObjectName'.lower()):
    print(item.text)
Output:
ALDBROUGH AVONMOUTH BACTON BBL BACTON IC BACTON PERENCO BACTON SEAL BACTON SHELL BARROW SOUTH DYNEVOR ARMS EASINGTON DIMLINGTON EASINGTON LANGELED EASINGTON ROUGH ST .......
Reply


Messages In This Thread
RE: Cant set api response as tree in Element tree - by snippsat - Aug-03-2018, 10:05 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How and where to store a data for path tree? zayacEBN 1 2,008 Aug-21-2020, 10:14 PM
Last Post: Larz60+
  How to display XML tree structure with Python? sonicblind 13 42,579 Aug-12-2020, 02:05 PM
Last Post: mreshko
  Form add tree comments with mptt m0ntecr1st0 1 2,581 Feb-23-2019, 01:50 PM
Last Post: m0ntecr1st0
  [Tkinter] Does anybody know that how to edit tree view on tkinter Prince_Bhatia 11 14,499 Jul-31-2018, 01:37 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020