2015-10-04 44 views
0
for (int i = 0; i < numberoflinks; i++) 
{ 
    string downloadString = client.DownloadString(mainlink+i+".html"); 
    var document = new HtmlWeb().Load(url); 
    var urls = document.DocumentNode.Descendants("img") 
         .Select(e => e.GetAttributeValue("src", null)) 
         .Where(s => !String.IsNullOrEmpty(s)) 
}  

問題是,HtmlWeb()。加載需要一個HTML網址,但我想加載字符串downloadString已經在裏面的HTML內容。我怎樣才能提取使用htmlagilitypack從字符串與HTML內容鏈接?

更新:

我現在嘗試這樣做:

for (int i = 0; i < numberoflinks; i++) 
      { 

       string downloadString = client.DownloadString(mainlink+i+".html"); 
       HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument(); 
       document.Load(downloadString); 
       var urls = document.DocumentNode.Descendants("img") 
               .Select(e => e.GetAttributeValue("src", null)) 
               .Where(s => !String.IsNullOrEmpty(s)); 
      } 

但我就行了越來越異常:路徑

document.Load(downloadString); 

非法字符

我」什麼想要做是從每一個環節下載/提取所有.JPG圖像。 沒有先下載鏈接到硬盤,但將內容下載到一個字符串提取物在這個網站以.jpg結尾的所有圖片鏈接然後下載JPG的。

回答

1

您應該能夠使用的HtmlDocumentLoadHtml()方法來處理HTML的字符串。

從源代碼:

public void LoadHtml(string html)

載荷從指定字符串的HTML文檔。

param name="html"

字符串包含的HTML文件來加載。可能不爲null。

Load方法需要一個文件名,這是關於illegal characters in path的消息的原因。

相關問題