Mary
Mary

Reputation: 163

How to download csv ata from website using Python

I'm trying to automatically download data from the following website; however I just get the html and no data: http://tcplus.com/GTN/OperationalCapacity#filter.GasDay=02/02/19&filter.CycleType=1&page=1&sort=LocationName&sort_direction=ascending

import csv
import urllib2

downloaded_data  = urllib2.urlopen('http://tcplus.com/GTN/OperationalCapacity#filter.GasDay=02/02/19&filter.CycleType=1&page=1&sort=LocationName&sort_direction=ascending')
csv_data = csv.reader(downloaded_data)

for row in csv_data:
    print row

Upvotes: 2

Views: 84

Answers (1)

andreilozhkin
andreilozhkin

Reputation: 535

The code below will only fetch data from provided url, but if you tweak parameters you can get other reports as well.

import requests

parameters = {'serviceTypeName': 'Ganesha.InfoPost.Service.OperationalCapacity.OperationalCapacityService, Ganesha.InfoPost.Service',
             'filterTypeName': 'Ganesha.InfoPost.ViewModels.GasDayAndCycleTypeFilterViewModel, Ganesha.InfoPost',
             'templateType': 6,
             'exportType': 1,
             'filter.GasDay': '02/02/19',
             'filter.CycleType': 1}

response = requests.post('http://tcplus.com/GTN/Export/Generate', data=parameters)

with open('result.csv', 'w') as f:
    f.write(response.text)

Upvotes: 1

Related Questions