简体   繁体   English

使用C#从静态链接下载文件

[英]Downloading files from static links with C#

I am needing to download a file from this page when the files are updated each quarter. 每季度更新文件时,我需要从此页面下载文件。 I am building an automated process to use the data from one of the files and I would like to automate the download as well. 我正在构建一个自动化过程来使用其中一个文件的数据,我也想自动下载。

What are my options? 我有什么选择? is there any way easier than screen scraping? 有没有比屏幕抓取容易的方法? the links are said to be static 链接被认为是静态的

If they are really static (ie never change), you could do something like this: 如果它们真的是静态的(即永远不会改变),你可以这样做:

var links = new Dictionary<string, string>
            {
                { "http://fhfa.gov/weblink/POSummary.xls", @"c:\temp\POSummary.xls" },
                { "...", "..."}
            };
var webClient = new WebClient();
foreach (var kvp in links)
{
    webClient.DownloadFile(kvp.Key, kvp.Value);
}

Or with one file even easier: 或者使用一个文件更容易:

webClient.DownloadFile("[address]", "[fileName]");

is there any way easier than screen scraping? 有没有比屏幕抓取容易的方法?

No, there isn't, unless the site provides you with an API that would allow you to return the available files to be downloaded in for example JSON format. 不,没有,除非该站点为您提供了一个API,允许您返回可下载的可用文件,例如JSON格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM