2012-07-20 56 views
0

我從網頁中提取內容。在網頁中,電話號碼和電子郵件ID等信息存儲在圖像中。我想提取圖像以及該表格內的文字。在輸出字符串中,我希望輸出的方式與使用圖像和文本的網頁中顯示的方式相同。使用htmlagilitypack提取文本和圖像

以下是網頁內容。

<table> 
<tr> 
    <td>text</td> 
    <td><img src="" /></td> 
</tr> 
<tr> 
    <td>text</td> 
    <td><img src="" /></td> 
</tr> 
<tr> 
    <td>text</td> 
    <td><img src="" /></td> 
</tr> 
</table> 

我可以得到文字和圖像中提取這樣的:

文本IMG

文本IMG

文本IMG

回答

0

試試這個

foreach (HtmlNode img in root.SelectNodes("//img")) 
{ 
    string att = img.Attributes["src"].Value; 
    anchorTags.Add(att); 
} 
+0

我編輯了這個問題。請看看它 – Maddy 2012-07-20 09:42:51

1
HtmlDocument doc = new HtmlDocument(); 
doc.Load("file.htm"); 
HtmlNode imgNode = doc.DocumentElement.selectSingleNode("/table/tr/td/img"); 

//Just get Images only 
foreach (HtmlNode img in doc.DocumentElement.SelectNodes("//img")) 
{ 
    string imgSrc = img.Attributes["src"].Value; 
} 

//get td's and ignore img in it 
foreach (HtmlNode td in doc.DocumentElement.SelectNodes("//td")) 
{ 
    HtmlNode img = td.ChildNodes["img"]; 
    if(img == null) 
    { 
    string tdText = td.InnerText; 
    } 
} 

//Get Images that have style attribute 
foreach (HtmlNode img in doc.DocumentElement.SelectNodes("//img[@style]")) 
{ 
    string style = img.Attributes["style"].Value.ToLower(); 
    style = style.Replace("background:url('", ""); 
    style = style.Replace("')", ""); 
//now you have the image url from the background 

} 
+0

我編輯過這個問題。請看看它。 – Maddy 2012-07-20 09:42:42

+0

@ user1516690請參閱我的更新回答 – HatSoft 2012-07-20 10:25:43

+0

謝謝HatSoft。 DocumentElement支持我的代碼。我嘗試使用DocumentNode。我還有一個問題,如何提取img標籤的背景圖像並存儲在我的系統中。 – Maddy 2012-07-20 11:35:08

相關問題