开发者

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

How could I download a file like .doc , .pdf from internet to my h开发者_JS百科ard drive using c#


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


The most simple way is use WebClient.DownloadFile


use the WebClient class:

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

Edit based on comments:

Based on your comments I think what you are trying to do is download i.e. PDF files that are linked to from an HTML page. In that case you can

  1. Download the page (with WebClient, see above)

  2. Use the HtmlAgilityPack to find all the links within the page that point to pdf files

  3. Download the pdf files

i am developing a crawler were if i specify a keyword for eg:SHA algorithm and i select the option .pdf or .doc from the crawler it should download the file with selected format in to a targeted folder ..

Based on your clarification this is a solution using google to get the results of the search:

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));
        }
    }
}

In general though you should ask a question related to a particular problem you have with a given implementation that you already have - based on your question you are very far off being able to implement a standalone crawler.


Use WebClient.DownloadFile() from System.Net


Using 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);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜