wsparser.py 897 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import urllib.request
  2. from lxml import etree # @UnresolvedImport
  3. class WSParser():
  4. """ Parser for a web service """
  5. def __init__(self, url):
  6. self._url = url
  7. self._data = None
  8. @property
  9. def url(self):
  10. return self._url
  11. def parse(self):
  12. strxml = urllib.request.urlopen(self._url).read()
  13. self._data = etree.fromstring(strxml)
  14. def __iter__(self):
  15. if self._data is None:
  16. self.parse()
  17. return (node.attrib for node in self._data)
  18. # OU
  19. #
  20. # from _io import BytesIO
  21. # URL = 'http://localhost:2890/public/WsPDE.asmx/GetPDETitres'
  22. #
  23. # data = urllib.request.urlopen(URL).read()
  24. #
  25. # for _, element in etree.iterparse(BytesIO(data)):
  26. # print(element.attrib)
  27. if __name__ == "__main__":
  28. ws_url = 'http://localhost:2890/public/WsPDE.asmx/GetPDETitres'
  29. for elt in WSParser(ws_url):
  30. print(elt)