如何刮
使用HtmlAgilityPack C#

asp.net c# html html-agility-pack

我想從電影網站上抓取數據,我會廢棄電影時間表和電影Title.and我不知道如何編寫查詢抓取這個html <div class="content" id="getSh">

<div class="container">
          <div class="content" id="getSh"><ul class="ctr"><li class="ctrl">Cinema 1</li>
          <li class="ctrr">09, Mar</li><li class="cl"></li></ul>
          <ul class="col_row"><li class="col"><a href="#">3:15 pm</a></li>
          <li class="col cb"><a href="/movies/detail/299">The Second Best Exotic Marigold Hotel 
          <span class="blue">Digital 2D</span></a></li><li class="col cc"><a href="#">--</a>
          </li><li class="cl"></li></ul> <ul class="col_row"><li class="col"><a href="#">6:15 pm</a
          li><li class="col cb"><a href="/movies/detail/307">Focus <span class="blue">Digital 2D
          </span><span class="red">Adults Only</span></a></li><li class="col cc"><a href="#">--
          </a></li><li class="cl"></li></ul> <ul class="col_row"><li class="col">
          <a href="#">8:45 pm</a></li><li class="col cb"><a href="/movies/detail/266">
          Kingsman: The Secret Service <span class="blue">Digital 2D</span><span class="red">
          Adults Only</span></a></li><li class="col cc"><a href="#">--</a></li><li class="cl">
          </li></ul><ul class="col_row col_m"><li class="col"><a href="#">11:45 pm</a></li>
          <li class="col cb"><a href="/movies/detail/267">Badlapur <span class="blue">Digital 2D
          </span></a></li><li class="col cc"><a href="#">--</a></li><li class="cl">
          </li></ul><ul class="ctr"><li class="ctrl">Cinema 2</li><li class="ctrr">09, Mar</li>
          <li class="cl"></li></ul> <ul class="col_row"><li class="col"><a href="#">3:30 pm</a>
          </li><li class="col cb"><a href="/movies/detail/307">Focus <span class="blue">Digital 
          </span><span class="red">Adults Only</span></a></li><li class="col cc"><a href="#">--<
          /a></li><li class="cl"></li></ul> <ul class="col_row"><li class="col"><a href="#">6:00
          pm</a></li><li class="col cb"><a href="/movies/detail/266">Kingsman: The Secret Service
          <span class="blue">Digital 2D</span><span class="red">Adults Only</span></a></li>
          <li class="col cc"><a href="#">--</a></li><li class="cl"></li></ul> <ul class="col_row">
          <li class="col"><a href="#">9:00 pm</a></li><li class="col cb"><a href="/movies/detail/307">
          Focus <span class="blue">Digital 2D</span><span class="red">Adults Only</span></a></li>
          <li class="col cc"><a href="#">--</a></li><li class="cl"></li></ul><ul class="col_row col_m">
          <li class="col"><a href="#">11:30 pm</a></li><li class="col cb"><a href="/movies/detail/266">
          Kingsman: The Secret Service <span class="blue">Digital 2D</span><span class="red">Adults Only
          </span></a></li><li class="col cc"><a href="#">--</a></li><li class="cl"></li></ul><ul class="
          ctr"><li class="ctrl">Cinema 3</li><li class="ctrr">09, Mar</li><li class="cl"></li></ul>
          <ul class="col_row"><li class="col"><a href="#">3:45 pm</a></li><li class="col cb"><
          a href="/movies/detail/321">Hey Bro <span class="blue">Digital 2D</span></a></li><
          li class="col cc"><a href="#">--</a></li><li class="cl"></li></ul> <ul class="col_row"><
          li class="col"><a href="#">6:30 pm</a></li><li class="col cb"><a href="/movies/detail/328">D
          irty Politics <span class="blue">Digital 2D</span><span class="red">Adults Only</span>
          </a></li><li class="col cc"><a href="#">--</a></li><li class="cl"></li></ul> 
          <ul class="col_row"><li class="col"><a href="#">9:30 pm</a></li><li class="col cb">
          <a href="/movies/detail/321">Hey Bro <span class="blue">Digital 2D</span></a></li><
          li class="col cc"><a href="#">--</a></li><li class="cl"></li></ul><ul class="col_row col_m">
          <li class="col"><a href="#">12:15 am</a></li><li class="col cb"><a href="/movies/detail/328"
          >Dirty Politics <span class="blue">Digital 2D</span><span class="red">Adults Only</span></a>

          </li><li class="col cc"><a href="#">--</a></li><li class="cl"></li></ul><ul class="ctr">
          <li class="ctrl">Cinema 4</li><li class="ctrr">09, Mar</li><li class="cl"></li></ul> 
          <ul class="col_row"><li class="col"><a href="#">3:00 pm</a></li><li class="col cb">
          <a href="/movies/detail/295">The SpongeBob Movie: Sponge Out of Water  <span class="blue">D
          igital 3D</span></a></li><li class="col cc"><a href="#">--</a></li><li class="cl"></li>
          </ul> <ul class="col_row"><li class="col"><a href="#">5:15 pm</a></li><li class="col cb">
          <a href="/movies/detail/300">Paddington <span class="blue">Digital 2D</span></a></li>
          <li class="col cc"><a href="#">--</a></li><li class="cl"></li></ul> <ul class="col_row"><
          li class="col"><a href="#">7:30 pm</a></li><li class="col cb"><a href="/movies/detail/297">
          Unbroken <span class="blue">Digital 2D</span></a></li><li class="col cc"><a href="#">--</a>
          </li><li class="cl"></li></ul><ul class="col_row col_m"><li class="col"><a href="#">10:30 pm
          </a></li><li class="col cb">
          <a href="/movies/detail/299">The Second Best Exotic Marigold Hotel <span class="blue">Digital 2D<
          /span></a></li><li class="col cc"><
          a href="#">--</a></li><li class="cl"></li></ul><ul class="ctr">
          <li class="ctrl">Royal Cinema</li><li class="ctrr">09, Mar</li>
          <li class="cl"></li></ul> <ul class="col_row"><li class="col"><
          a href="#">3:05 pm</a></li><li class="col cb"><a href="/movies/detail/328">Dirty Politics <
          span class="blue">Digital 2D</span><span class="red">Adults Only</span></a></li><li class="col cc">
          <a href="#">--</a></li><li class="cl"></li></ul> <ul class="col_row"><li class="col"><a href="#">
          6:05 pm</a></li><li class="col cb"><a href="/movies/detail/307">Focus <span class="blue">Digital 2D
          </span><span class="red">Adults Only</span></a></li><li class="col cc"><a href="#">--</a></li>
          <li class="cl"></li></ul><ul class="col_row col_m"><li class="col"><a href="#">8:30 pm</a></li>
          <li class="col cb"><a href="/movies/detail/299">The Second Best Exotic Marigold Hotel
          <span class="blue">Digital 2D</span></a></li><li class="col cc"><a href="#">--</a></li>
          <li class="cl"></li></ul></div>
        </div>

我使用這個C#代碼來提取不起作用的數據

HtmlNode htmlNode = document.DocumentNode.SelectSingleNode("//div[@id='customScrollBox']");


        List<string> movieList = new List<string>();


        foreach (HtmlNode heading in htmlNode.SelectNodes("//ul[@class='col_row']"))
        {
            movieList.Add(heading.InnerText);

        }

我想要這個輸出電影室=電影院1電影名稱=第二個最好的異國萬壽菊酒店和時間表

一般承認的答案

從我收集到的你想要獲得電影名稱?如果是這樣,下面的代碼應該這樣做:

    foreach (HtmlNode heading in htmlNode.SelectNodes("//ul[@class='col_row']")
    {
        var heading = heading.SelectSingleNode(".//li[@class='col cb']/a").InnerText;
        //I Presume you want other fields here?
    }



許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因