Skip to main content
 首页 » 编程设计

python之如何使用Python从网站下载csv数据

2024年11月24日9飞鱼

我正在尝试从以下网站自动下载数据;但是我只得到 html 而没有数据: 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 

请您参考如下方法:

下面的代码只会从提供的 url 中获取数据,但如果您调整参数,您也可以获得其他报告。

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)