【问题标题】:How would you automate downloading a file from a site everyday using python? [closed]您将如何每天使用 python 自动从站点下载文件? [关闭]
【发布时间】:2017-06-26 01:02:36
【问题描述】:

如何自动从该页面https://www.nseindia.com/products/content/equities/equities/homepage_eq.htm 下载文件,我使用 urllib 使用 python 进行了尝试。

import urllib

testfile = urllib.URLopener()
testfile.retrieve("https://www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8")

即使这似乎不起作用,我也不知道为什么,但是你如何从这样的网站下载文件并将 uri 更改为文件,只需查看模式并使用 像上述情况一样添加代码日期?为什么上面的代码不起作用?

【问题讨论】:

    标签: python file download web-scraping


    【解决方案1】:

    您不需要使用urllib.URLopener(),只需使用urllib.urlretrieve(),如下所示:

    import urllib
    urllib.urlretrieve("https://www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8")
    

    你也可以使用wget:

    import wget
    wget.download("https://www.nseindia.com/products/content/historical/EQUITIES/2017/JUN?cm23JUN2017bhav.csv.zip8", "file.zip8")
    

    关于每天自动下载,您可以使用 windows 调度程序,或使用loop,如下所示:

    import urllib
    import time
    while True:
        urllib.urlretrieve('file', 'file')
        time.sleep(86400) # 86400 seconds = 24 hours.
    

    【讨论】:

    • 谢谢哥们,如果您看到文件的 uri 每天都在更改?你是怎么更新的!
    • 你到底想更新什么?
    • 不,我的意思是要下载一个文件,你必须有一个特定的 URI 对吗?在我提到的网站中,每天都会发生变化。我只想知道如何在中编码
    • 你知道模式吗?还是您每天都知道该 URL 并想更新它?
    猜你喜欢
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 2016-07-09
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多