Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Scraper
- {
- public static string Scrape(string userName, string date)
- {
- //example: http://www.myfitnesspal.com/food/diary/chuckgross?date=2015-03-05
- //var url = "http://www.myfitnesspal.com/food/diary/" + userName + "?date=" + date;
- var url = "http://www.myfitnesspal.com/food/diary/chuckgross?date=2015-03-05";
- string results;
- using (var client = new WebClient())
- {
- results = client.DownloadString(url);
- }
- var nutritionTable = GetNutritionTable(results);
- return results;
- }
- private static string GetNutritionTable(string html)
- {
- var document = new HtmlDocument();
- document.LoadHtml(html);
- //get the table we care about
- HtmlNode primaryTable = document.DocumentNode.Descendants("table").First(d => d.Attributes.Contains("class") && d.Attributes["class"].Value == "table0");
- //get the totalsRow
- //<tr class="total">
- // <td class="first">Totals</td>
- // <td>0</td>
- // <td>0</td>
- // <td>0</td>
- // <td>0</td>
- // <td>0</td>
- //<td class="empty"></td>
- //</tr>
- HtmlNode totalsRow = primaryTable.Descendants("tr")
- .First(d => d.Attributes.Contains("class") && d.Attributes["class"].Value == "total");
- HtmlNodeCollection totalCells = totalsRow.SelectNodes(".//td[not(@class='first') and not(@class='empty')]");
- var totalCellsHtml = totalCells.Aggregate(string.Empty, (current, label) => current + label.OuterHtml);
- //get the HeaderRow
- //<tfoot>
- // <tr>
- // <td class="first"></td>
- // <td class="alt">Calories</td>
- // <td class="alt">Protein</td>
- // <td class="alt">Carbs</td>
- // <td class="alt">Fat</td>
- // <td class="alt">Fiber</td>
- // <td class="empty"></td>
- // </tr>
- //</tfoot>
- HtmlNode headerRow = primaryTable.Descendants("tfoot")
- .First()
- .Descendants("tr")
- .First();
- HtmlNodeCollection headerCells = headerRow.SelectNodes(".//td[@class='alt']");
- var headerCellsHtml = headerCells.Aggregate(string.Empty, (current, label) => current + label.OuterHtml);
- return BuildNutritionTable(totalCellsHtml, headerCellsHtml);
- }
- private static string BuildNutritionTable(string totalsHtml, string labelsHtml)
- {
- labelsHtml = Regex.Replace(labelsHtml, "<td class=\\\"alt\\\">", "<th>").Replace("</td>", "</th>");
- StringBuilder table = new StringBuilder("<table class='resultsTable'><tr class='labels'>");
- table.Append(labelsHtml);
- table.Append("</tr><tr class='resultsTotals'>");
- table.Append(totalsHtml);
- table.Append("</tr></table>");
- var results = table.ToString();
- return results;
- }
- public class NutritionRecord
- {
- public string Calories { get; set; }
- public string Protein { get; set; }
- public string Fat { get; set; }
- public string Carbs { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement