2

ウィキペディアの 1 つのテーブルを xml ファイルに入れ、それを C# に解析したいと考えています。出来ますか?はいの場合、タイトルジャンルの列のみを xml に保存できますか?

HtmlWeb web = new HtmlWeb();
HtmlDocument doc = web.Load("http://en.wikipedia.org/wiki/2012_in_film");

HtmlNode node = doc.DocumentNode.SelectSingleNode("//table[@class='wikitable']");
4

3 に答える 3

1

次のコードを使用できます。検索したい html タグを検索し、正規表現を作成して残りのデータを解析します。このコードは、幅 150 のテーブルを検索し、すべての url/nav url を取得します。

HtmlElementCollection links = webBrowser1.Document.GetElementsByTagName("table"); //get collection in link
                {
                    foreach (HtmlElement link_data in links) //parse for each collection
                    {
                        String width = link_data.GetAttribute("width");
                        {
                            if (width != null && width == "150")
                            {
                                Regex linkX = new Regex("<a[^>]*?href=\"(?<href>[\\s\\S]*?)\"[^>]*?>(?<Title>[\\s\\S]*?)</a>", RegexOptions.IgnoreCase);
                                MatchCollection category_urls = linkX.Matches(link_data.OuterHtml);
                                if (category_urls.Count > 0)
                                {
                                    foreach (Match match in category_urls)
                                    {
                                           //rest of the code
                                    }
                                }
                             }
                         }
                     }
                }
于 2012-12-26T05:21:01.343 に答える
1

Web ブラウザを使用できます。

//First navigate to your address
 webBrowser1.Navigate("http://en.wikipedia.org/wiki/2012_in_film");
        List<string> Genre = new List<string>();
        List<string> Title = new List<string>();
  //When page loaded
  foreach (HtmlElement table in webBrowser1.Document.GetElementsByTagName("table"))
            {
                if (table.GetAttribute("className").Equals("wikitable"))
                {
                    foreach (HtmlElement tr in table.GetElementsByTagName("tr"))
                    {
                        int columncount = 1;
                        foreach (HtmlElement td in tr.GetElementsByTagName("td"))
                        {
                            //Title
                            if (columncount == 4)
                            {
                                Title.Add(td.InnerText);
                            }
                            //Genre
                            if (columncount == 7)
                            {
                                Genre.Add(td.InnerText);
                            }
                            columncount++;
                        }

                    }
                }
            }

これで、2 つのリスト (ジャンルとタイトル) ができました。それらを単純にxmlファイルに変換できます

于 2012-12-26T05:24:15.893 に答える