Skip to content

Instantly share code, notes, and snippets.

@compil3
Created October 31, 2013 06:59
Show Gist options
  • Save compil3/7245338 to your computer and use it in GitHub Desktop.
Save compil3/7245338 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using HtmlAgilityPack;
using System.Xml;
namespace NHLScraper
{
public class Converter
{
private string Source;
private static string Filename = "[" + DateTime.Now.ToShortDateString() + " Habs Stats].xml";
public void StatsParser(string Source)
{
this.Source = Source;
this.ParseHtml();
}
public void ParseHtml()
{
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(Source);
// use the right XPath rather than looping manually
var cells = htmlDoc.DocumentNode
.SelectNodes("/html/body/div[2]/div[4]/div/div[3]/table/tbody/tr/td[2]/a")
.Select(node => node.InnerText.Trim())
.ToList();
var elementNames = new[] { "Name", "Team", "Pos", "GP", "G", "A", "Pts", "PlusMinus", "PIM", "PP", "SH", "GW", "OT", "Shots", "ShotPctg", "TOIPerGame", "ShiftsPerGame", "FOWinPctg" };
var xmlDoc = new XElement("Stats", new XAttribute("Date", DateTime.Now.ToShortDateString()),
new XElement("Player", new XAttribute("Rank", cells.First()),
// generate the elements based on the parsed cells
cells.Skip(1)
.Zip(elementNames, (Value, Name) => new XElement(Name, Value))
.Where(element => !String.IsNullOrEmpty(element.Value))
)
);
xmlDoc.Save("H:\xmlDocs");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment