Skip to content

Instantly share code, notes, and snippets.

@glowinthedark
Created April 11, 2019 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glowinthedark/ee1f74f4c06cf750dfd2b1abd43333f3 to your computer and use it in GitHub Desktop.
Save glowinthedark/ee1f74f4c06cf750dfd2b1abd43333f3 to your computer and use it in GitHub Desktop.
Python Example Web Service using suds and lxml
import suds
import sys
import lxml.etree as etree
def xml_pretty_print(doc):
return etree.tostring(doc, pretty_print = True)
url = "http://www.webservicex.net/stockquote.asmx?wsdl"
# get ticker symbol as command line arg or use default
ticker_symbol = len(sys.argv) > 1 and sys.argv[1] or 'GOOG'
print 'opening WSDL: %s...' % url,
client = suds.client.Client(url)
print 'Ok'
print 'Sending request...',
xml_response = client.service.GetQuote(ticker_symbol)
print 'Ok'
doc = etree.fromstring(xml_response)
# print formatted XML response
# print xml_pretty_print(doc)
stock = doc[0] #get first node
for elem in stock:
print '%s: %s' % (elem.tag.rjust(16), elem.text)
## SAMPLE OUTPUT ##
#Sending request... Ok
# Symbol: GOOG
# Last: 630.69
# Date: 12/20/2011
# Time: 10:07am
# Change: +8.86
# Open: 628.38
# High: 631.22
# Low: 627.99
# Volume: 432221
# MktCap: 204.3B
# PreviousClose: 621.83
#PercentageChange: +1.42%
# AnnRange: 473.02 - 642.96
# Earns: 29.337
# P-E: 21.20
# Name: Google Inc.
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment