I am trying to create a news agent to get the news from the websites.so i have to use a html parser like HtmlAgilityPack .so here you ca see my code :
public async void parsing(string website)
{
HttpClient http = new HttpClient();
var response = await http.GetByteArrayAsync(website);
String source = Encoding.GetEncoding("utf-8").GetString(response, 0, response.Length - 1);
source = WebUtility.HtmlDecode(source);
HtmlDocument resultat = new HtmlDocument();
resultat.LoadHtml(source);
List<HtmlNode> toftitle = resultat.DocumentNode.Descendants().Where
(x => (x.Name == "div" && x.Attributes["class"] != null && x.Attributes["class"].Value.Contains("latest-news"))).ToList();
var li = toftitle[0].Descendants("li").ToList();
foreach (var item in li)
{
var link = item.Descendants("a").ToList()[0].GetAttributeValue("href", null);
var img = item.Descendants("img").ToList()[0].GetAttributeValue("src", null);
}
}
here is my html code that should be parsed :
<a href="/news?p_p_id=56_INSTANCE_tVzMoLp4zfGh&_56_INSTANCE_tVzMoLp4zfGh_mode=news&_56_INSTANCE_tVzMoLp4zfGh_newsId=3153832&p_p_state=maximized">› پانل «بررسي سازوکارهاي تأمين منابع مالي براي توسعۀ فناوري» بهعنوان پانل برتر پنجمين کنفرانس بينالمللي و نهمين کنفرانس ملي مديريت فناوري معرفي شد</a>
<a href="/news?p_p_id=56_INSTANCE_tVzMoLp4zfGh&_56_INSTANCE_tVzMoLp4zfGh_mode=news&_56_INSTANCE_tVzMoLp4zfGh_newsId=3135970&p_p_state=maximized">› فرآیند و فرم درخواست استفاده از تسهیلات حمایتی بلاعوض صندوق نوآوری و شکوفایی جهت حضور شرکت های دانش بنیان در جایزه ملی مدیریت فناوری و نوآوری</a>
So the problem is i can get the href link but not href value .I mean i can get the news url but not title پانل «بررسي سازوکارهاي تأمين منابع مالي براي توسعۀ فناوري» بهعنوان پانل برتر پنجمين کنفرانس بينالمللي و نهمين کنفرانس ملي مديريت فناوري معرفي شد.
How can i get that ?
you can use like this :
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(result);
foreach (HtmlNode link in doc.DocumentNode.SelectNodes("a"))
{
string value = link.InnerText; // here you can get href value
}
I just should use this code to get the innertext of href :
string tistle = item.Descendants("a").ToList()[0].InnerText;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With