разбирать элементы таблицы html в c # - PullRequest
0 голосов
/ 02 июня 2018

Привет, у меня есть HTML-таблица в следующем формате:

<table id="archive_regulation" class="table table-striped table-hover">
    <thead>
    <tr>
        <th>row</th>
        <th>category</th>
        <th>title</th>
        <th>date</th>
        <th>type</th>
        <th>sub type</th>
        <th>download</th>
    </tr>
    </thead>
            <tr>
            <td>1</td>
            <td>test</td>
            <td>some thing for test</td>
            <td>۱۳۹۷/۰۳/۱۲</td>
            <td>general</td>
            <td>other</td>
            <td>
                <a class="btn btn-info" href="http://10.30.170.46/portal/fileLoader.php?code=91a1135b8898b8db76ada81527923329">
                    <span aria-hidden="true" class="glyphicon glyphicon-download"></span>
                    <b> download</b>
                </a>
            </td>
        </tr>
                <tr>
            <td>2</td>
            <td>something for test</td>
            <td>another thing for test</td>
            <td>۱۳۹۷/۰۳/۱۲</td>
            <td>vip</td>
            <td>new</td>
            <td>
                <a class="btn btn-info" href="http://10.30.170.46/portal/fileLoader.php?code=164f564d81e7f3cc3c18091527922677">
                    <span aria-hidden="true" class="glyphicon glyphicon-download"></span>
                    <b> download</b>
                </a>
            </td>
        </tr>
        </table>

Теперь я хочу получить такие элементы:

1
тест
кое-что дляtest
۱۳۹۷/۰۳/۱۲
общее
другое
http://10.30.170.46/portal/fileLoader.php?code=91a1135b8898b8db76ada81527923329
2
что-то для теста
другое для теста
۱۳۹۷/۰۳/۱۲
vip
new
http://10.30.170.46/portal/fileLoader.php?code=164f564d81e7f3cc3c18091527922677

Я использовал htmlagilitypack, но шаблон неправильный

 WebClient webClient = new WebClient();
        string page = webClient.DownloadString("http://5743.zanjan.medu.ir/regulation/archive?ocode=100038170");
        HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
        doc.LoadHtml(page);
        foreach (HtmlNode table in doc.DocumentNode.SelectNodes("//table"))
        {
            Console.WriteLine("Found: " + table.Id);
            foreach (HtmlNode row in table.SelectNodes("tr"))
            {
                Console.WriteLine("row");
                foreach (HtmlNode cell in row.SelectNodes("th|td"))
                {
                    Console.WriteLine("cell: " + cell.InnerText);
                }
            }
        }

Что мне делать?

1 Ответ

0 голосов
/ 02 июня 2018

Код в вопросе отличается от того, что вы спрашиваете.

Приведенный ниже код возвращает запрашиваемый вывод.обратите внимание, что URL, с которого вы получаете HTML, не работает для меня.(не забудьте использовать System.Linq)

WebClient webClient = new WebClient();            
string page = webClient.DownloadString("http://5743.zanjan.medu.ir/regulation/archive?ocode=100038170");

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(page);

var output = "";

var table = doc.GetElementbyId("archive_regulation");

foreach (HtmlNode td in table.Descendants("td"))
{
    var anchors = td.Descendants("a");

    if (anchors.Count() > 0)
        output += anchors.First().GetAttributeValue("href", null);
    else
        output += td.InnerText;

    output += "\n";
}

Console.WriteLine(output);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...