如何使用Python从服务器/网站导入文件/列表

时间:2012-11-05 00:36:27

标签: python

我想通过使用python必须从服务器/网站获取的标准txt文件来获得一个永远更新的列表。

实施例

    women =["Dare1","Dare2","Dare3","Dare4","Dare5","Dare6"]

   men = ["Dare1","Dare2","Dare3","Dare4","Dare5","Dare6"]

然后重新打开程序后,它会获取更新的txt

    women =["Dare1","Dare2","Dare3","Dare4","Dare5","Dare6","Dare7"]

   men = ["Dare1","Dare2","Dare3","Dare4","Dare5","Dare6"]

1 个答案:

答案 0 :(得分:0)

那么这个文本文件会在目标服务器上自动生成/更新吗?如果您知道显示此文本文件的URL,就像使用python的urlretrieve模块实现urllib来检索它并在您自己的系统上实现cron一样简单运行那个python脚本。

以下是其用法示例: -

fName = "CFE_{0}{1}_VX.csv".format(m_codes[month],str(year)[-2:])
save_path = os.path.join(path, fName)
if os.path.exists(save_path) or forceDownload:
    print 'File already downloaded, skipping'
    return

urlStr = "http://cfe.cboe.com/Publish/ScheduledTask/MktData/datahouse/{0}".format(fName)
print 'Getting: %s' % urlStr
try:
    print save_path
    urlretrieve(urlStr, save_path)
except Exception as e:
    print e

在这种情况下,我正在从Chicago Board Options Exchange下载csv文件。