繁体   English   中英

我怎样才能使用c#将.doc或.pdf等文件从互联网下载到我的硬盘

[英]How could I download a file like .doc or .pdf from internet to my harddrive using c#

我怎样才能使用c#将.doc,.pdf等文件从互联网下载到我的硬盘上

using (var client = new System.Net.WebClient())
{
    client.DownloadFile( "url", "localFilename");
}

使用WebClient类:

using(WebClient wc = new WebClient())
wc.DownloadFile("http://a.com/foo.pdf", @"D:\foo.pdf");

根据评论进行编辑:

根据您的意见, 我认为您要做的是下载即从HTML页面链接到的PDF文件。 在那种情况下你可以

  1. 下载页面(使用WebClient,见上文)

  2. 使用HtmlAgilityPack查找页面中指向pdf文件的所有链接

  3. 下载pdf文件

我正在开发一个爬虫,如果我为例如:SHA算法指定一个关键字,我从爬虫中选择选项.pdf或.doc它应该将选定格式的文件下载到目标文件夹中。

根据您的澄清,这是一个使用谷歌获取搜索结果的解决方案:

DownloadSearchHits("SHA", "pdf");

...

public static void DownloadSearchHits(string searchTerm, string fileType)
{
    using (WebClient wc = new WebClient())
    {
        string html = wc.DownloadString(string.Format("http://www.google.com/search?q={0}+filetype%3A{1}", searchTerm, fileType));
        HtmlDocument doc = new HtmlDocument();
        doc.LoadHtml(html);
        var pdfLinks = doc.DocumentNode
                            .SelectNodes("//a")
                            .Where(link => link.Attributes["href"] != null 
                                   && link.Attributes["href"].Value.EndsWith(".pdf"))
                            .Select(link => link.Attributes["href"].Value)
                            .ToList();

        int index = 0;
        foreach (string pdfUrl in pdfLinks)
        {
            wc.DownloadFile(pdfUrl, 
                            string.Format(@"C:\download\{0}.{1}", 
                                            index++, 
                                            fileType));
        }
    }
}

一般情况下,您应该问一个与您已经拥有的特定实现相关的特定问题的问题 - 根据您的问题,您很难实现独立的爬虫。

最简单的方法是使用WebClient.DownloadFile

使用System.Net中的WebClient.DownloadFile()

使用WebClient.DownloadFile

http://msdn.microsoft.com/en-us/library/system.net.webclient.downloadfile.aspx

    using (var client = new WebClient())
    {
        var data = client.DownloadFile(url, filename);
    }

暂无
暂无

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

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