2012-06-15 41 views
1

我試圖通過網頁源代碼,將<img src="http://www.dot.com/image.jpg"添加到HtmlElementCollection。然後我試圖通過foreach循環遍歷元素集合中的每個元素並通過url下載圖像。用C掃描圖像#

這是我到目前爲止。我現在的問題是沒有什麼是下載,我不認爲我的元素被標籤名稱正確添加。如果他們是我似乎無法引用他們的下載。

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    public void button1_Click(object sender, EventArgs e) 
    { 
     string url = urlTextBox.Text; 
     string sourceCode = WorkerClass.ScreenScrape(url); 
     StreamWriter sw = new StreamWriter("sourceScraped.html"); 
     sw.Write(sourceCode); 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     string url = urlTextBox.Text; 
     WebBrowser browser = new WebBrowser(); 
     browser.Navigate(url); 
     HtmlElementCollection collection; 
     List<HtmlElement> imgListString = new List<HtmlElement>(); 
     if (browser != null) 
     { 
      if (browser.Document != null) 
      { 
       collection = browser.Document.GetElementsByTagName("img"); 
       if (collection != null) 
       { 
        foreach (HtmlElement element in collection) 
        { 
         WebClient wClient = new WebClient(); 
         string urlDownload = element.FirstChild.GetAttribute("src"); 
         wClient.DownloadFile(urlDownload, urlDownload.Substring(urlDownload.LastIndexOf('/'))); 
        } 
       } 
      } 
     } 
    } 
} 

}

+0

你試圖去通過網頁,並添加了...什麼? –

+0

檢查urlDownload值以獲取有效路徑。 – jac

回答

2

你稱之爲導航的人,你認爲文件已準備好遍歷並檢查圖像。但實際上需要一些時間來加載。您需要等到文檔加載完成。

添加事件DocumentCompleted到瀏覽器對象

browser.DocumentCompleted += browser_DocumentCompleted; 

實現它作爲

static void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
{ 
    WebBrowser browser = (WebBrowser)sender; 
    HtmlElementCollection collection; 
    List<HtmlElement> imgListString = new List<HtmlElement>(); 
    if (browser != null) 
    { 
     if (browser.Document != null) 
     { 
      collection = browser.Document.GetElementsByTagName("img"); 
      if (collection != null) 
      { 
       foreach (HtmlElement element in collection) 
       { 
        WebClient wClient = new WebClient(); 
        string urlDownload = element.GetAttribute("src"); 
        wClient.DownloadFile(urlDownload, urlDownload.Substring(urlDownload.LastIndexOf('/'))); 
       } 
      } 
     } 
    } 
} 
+0

這正是我所做的。有效。我正要發佈我自己的答案!大聲笑。 – Keith

+0

很高興聽到這個消息。接受答案中的一個,或者您可以發佈自己的答案,並接受答案,如果與此不同。 – Damith

+0

對不起。我沒有注意到有一個地方可以接受答案。我是新來的。 – Keith

0

看看Html Agility Pack

你需要做的是下載並解析HTML,然後處理你感興趣的元素。它是這類任務的好工具。

0

感興趣的人,這裏是解決方案。這正是達米斯所說的。我發現Html敏捷包相當破碎。那是我嘗試使用的第一件事。這最終成爲對我來說更可行的解決方案,這是我的最終代碼。

private void button2_Click(object sender, EventArgs e) 
    { 
     string url = urlTextBox.Text; 
     WebBrowser browser = new WebBrowser(); 
     browser.Navigate(url); 
     browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DownloadFiles); 
    } 

    private void DownloadFiles(object sender, WebBrowserDocumentCompletedEventArgs e) 
    { 

     HtmlElementCollection collection; 
     List<HtmlElement> imgListString = new List<HtmlElement>(); 

     if (browser != null) 
     { 
      if (browser.Document != null) 
      { 
       collection = browser.Document.GetElementsByTagName("img"); 
       if (collection != null) 
       { 
        foreach (HtmlElement element in collection) 
        { 
         string urlDownload = element.GetAttribute("src"); 
         if (urlDownload != null && urlDownload.Length != 0) 
         { 
          WebClient wClient = new WebClient(); 
          wClient.DownloadFile(urlDownload, "C:\\users\\folder\\location\\" + urlDownload.Substring(urlDownload.LastIndexOf('/'))); 
         } 
        } 
       } 
      } 
     } 
    } 
} 

}