import csv import requests def WriteSymbolsToCSVFile(csvWriter, response): count = 0 for symObj in response[1:]: if 'Attr' in symObj and 'Data' in symObj: symbol = symObj['Attr']['TickerSymbol'] description = symObj['Data'].get('IssueDescription', 'No description') csvWriter.writerow([symbol, description]) count += 1 return count def main(): fp = open('symbols.csv', 'w', newline='') csvWriter = csv.writer(fp) username = "username" password = "password" URL = "http://pxweb.dtn.com/PXWebSvc/PXServiceWeb.svc/GetQuoteSnap" paramters = { 'UserID' : username, 'Password' : password, 'Type' : 'IssueDescription', 'Symbol' : '*', 'SymbolLimit' : '4000', 'Format' : 'JSON' } totalSymbols = 0 responseObj = requests.get(URL, paramters) response = responseObj.json() metaData = response[0] totalSymbols += WriteSymbolsToCSVFile(csvWriter, response) print(f'Response received in {responseObj.elapsed.total_seconds()} seconds. Total symbols = {totalSymbols}') while 'PageToken' in metaData: paramters['PageToken'] = metaData['PageToken'] responseObj = requests.get(URL, paramters) response = responseObj.json() metaData = response[0] totalSymbols += WriteSymbolsToCSVFile(csvWriter, response) print(f'Response received in {responseObj.elapsed.total_seconds()} seconds. Total symbols = {totalSymbols}') if __name__ == '__main__': main()