2012-06-15 77 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('/'))); 
         } 
        } 
       } 
      } 
     } 
    } 
} 

}