C# Web Scraper Using HTMLAgilityPack – Part III

In this tutorial, I will show you how to read data from tables. Sometimes you have to develop a program that reads data from a table within an HTML Page. For example, reading jokes and its author from a site. Here’s a sample HTML page below named sample3.html.


<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>Sample 3</title>
</head>
<body>
<table>
<tr>
<td>Author1</td>
<td>Joke1</td>
</tr>
<tr>
<td>Author2</td>
<td>Joke2</td>
</tr>
</table>
</body>
</html>

When you are finished with the HTML page, create a new C# Project in Visual Studio. Select Console Application and name it GrabJokes. Type the codes below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;

namespace GrabJokes
{
class Program
{
static void Main(string[] args)
{
HtmlDocument doc = new HtmlDocument();
doc.Load(“C:\\Users\\allmankind\\Documents\\sample3.html”);
foreach (HtmlNode row in doc.DocumentNode.SelectNodes(“//tr”))
{
Console.Write(row.Elements(“td”).First().InnerText);
Console.Write(” – “);
Console.WriteLine(row.Elements(“td”).Last().InnerText);
}
Console.ReadKey();

}
}
}

Like the other tutorials in this series, similar codes are used. The difference is the code inside the foreach loop which selects tr and then selects the first-child td and outputs a hyphen then selects the last-child td.

Thank you for reading. Don’t forget to share and leave a comment.

Advertisements

27 thoughts on “C# Web Scraper Using HTMLAgilityPack – Part III

  1. Pingback: Homepage

  2. Hey there! Would you mind if I share your blog with my zynga group?
    There’s a lot of people that I think would really enjoy your content. Please let me know. Many thanks

  3. each time i used to read smaller articles or reviews that as well clear their motive,
    and that is also happening with this paragraph which I am reading at this place.

  4. Pingback: Upcoming Events

  5. Pingback: vracer race car simulator

  6. Pingback: taxi mallorca

  7. Pingback: buy movies online

  8. Pingback: useful link

  9. Pingback: yahoo

  10. Pingback: Earn Cash Prizes

  11. Pingback: old mobile movies

  12. Pingback: cycling Massa Marittima

  13. Pingback: Ganhe dinheiro na internet

  14. Pingback: get high pr backlinks for free

  15. Pingback: websites

  16. Pingback: best way to make money

  17. Pingback: sexy thai

  18. Pingback: Trabalho pela internet

  19. Pingback: mobile sites builder London

  20. Pingback: www.casinoscosmopol.com

  21. Pingback: translation, legal translation, uae translation agency, uae translation service, menu translation, press release translation, powerpoint translation, uae laws translation, gcc laws translation, arab countries laws translation, uae federal laws, uae local

  22. Pingback: online betting

  23. Pingback: Newcastle photographer

  24. You assume control on the process: instead of being on the mercy with the dealership, you are
    now in control in the finance process morrissey tickets 2014 tour in fact,
    this could be is more than a 500% in daily interest.

  25. Hi there, just wanted to tell you, I liked this post. It was funny.
    Keep on posting!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s