Парсинг с помощью HtmlAgilityPack - C#
Формулировка задачи:
Здравствуйте. Нужно спарсить со страницы два числа, которые стоят между тегов <span id="first_id">*первое число*<font> и <span id="second_id">*второе число*<font> и поместить их в две переменные (без разницы, string или int). Предпочтительней будет сделать это с помощью HtmlAgilityPack, но можно и по-другому. Ниже часть кода страницы.
Спасибо.
Листинг программы
- <body>
- ....
- <div class="class1">
- <div class="class2">
- <p>You have <span id="first_id">142 <font>points</font></span></p>
- <p>She has<span id="second_id">2324 <font>points</font></span></p>
- </div>
- <div id="div_id">
- <div style="padding: 40px 0 0 55px; font-size: 12px; text-align:center;">
- <strong>Your Flash Player plugin is outdated.</strong><br />
- Click <a href="/?detectflash=false">here</a>, to download the latest Adobe Flash Player plugin.
- </div>
- </div>
- </div>
- ...
- </body>
Решение задачи: «Парсинг с помощью HtmlAgilityPack»
textual
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using HtmlAgilityPack;
- using HtmlDocument = HtmlAgilityPack.HtmlDocument;
- using System.IO;
- namespace WinFormPars
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- try
- {
- example();
- }
- catch (Exception)
- {
- throw;
- }
- }
- void example()
- {
- string sURL;
- sURL = "http://www.yandex.ru";
- HtmlDocument doc = new HtmlDocument();
- var web = new HtmlWeb
- {
- AutoDetectEncoding = false,
- OverrideEncoding = Encoding.UTF8,
- };
- HtmlNodeCollection hnNode = doc.DocumentNode.SelectNodes("//table[@class='" + "b-stocks-i b-stocks-i-currency" + "']");
- if (hnNode != null)
- {
- foreach (var hn in hnNode)
- {
- string outputText = hn.OuterHtml;
- webBrowser1.DocumentText = outputText;
- richTextBox1.AppendText(outputText);
- }
- }
- else
- {
- MessageBox.Show("hnNode вернула null");
- }
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д