{"id":367,"date":"2017-05-10T04:25:06","date_gmt":"2017-05-09T19:25:06","guid":{"rendered":"http:\/\/marius.main.jp\/software\/blog\/?p=367"},"modified":"2020-03-25T18:19:35","modified_gmt":"2020-03-25T09:19:35","slug":"post-367","status":"publish","type":"post","link":"https:\/\/todosoft.net\/blog\/?p=367","title":{"rendered":"Html Agility Pack \u3092\u4f7f\u7528\u3057\u3066\u30b5\u30a4\u30c8\u306eHTML\u3092\u89e3\u6790\u3059\u308b"},"content":{"rendered":"\n<p> <a rel=\"noreferrer noopener\" href=\"http:\/\/html-agility-pack.net\/\" target=\"_blank\">Html Agility Pack<\/a> \u3092\u4f7f\u7528\u3059\u308b\u3068\u3001HTML\u30bf\u30b0\u304c\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u3088\u3046\u306b\u6271\u3048\u3001<br>\u7c21\u5358\u306b\u89e3\u6790\u3092\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u308b\u3002 <\/p>\n\n\n\n<!--more-->\n\n\n\n<pre class=\"wp-block-luxe-blocks-syntaxhighlighter line-numbers language-csharp\"><code class=\"language-csharp\">private void button1_Click(object sender, EventArgs e)\n{\n    string strSource = \"\";\n\n    \/\/ \u30bd\u30fc\u30b9\u306e\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\n    using (System.Net.WebClient objWebClient = new System.Net.WebClient())\n    {\n        objWebClient.Encoding = System.Text.Encoding.UTF8;\n        strSource = objWebClient.DownloadString(\"https:\/\/news.google.co.jp\/\");\n    }\n\n    \/\/ HTML\u306e\u89e3\u6790\n    HtmlAgilityPack.HtmlDocument objHTMLDocument = new HtmlAgilityPack.HtmlDocument();\n    objHTMLDocument.LoadHtml(strSource);\n\n    \/\/ \u5358\u4e00\u306e\u30bf\u30b0\u3092\u691c\u51fa\n    HtmlAgilityPack.HtmlNode objTopNewsNode = objHTMLDocument.DocumentNode.SelectSingleNode(\"\/\/div[contains(@class, 'section-ja_jp-t')]\");\n\n    HtmlAgilityPack.HtmlNode objTopNewsNameNode = objTopNewsNode.SelectSingleNode(\"*\/\/span[@class='section-name']\");\n    Console.WriteLine(\"TopNewsName:\" + objTopNewsNameNode.InnerText);\n\n    \/\/ \u8907\u6570\u306e\u30bf\u30b0\u3092\u691c\u51fa\n    HtmlAgilityPack.HtmlNodeCollection objNewsDataNodeCollection = objTopNewsNode.SelectNodes(\"*\/\/h2[@class='esc-lead-article-title']\/a[starts-with(@class, 'article')]\");\n\n    foreach (HtmlAgilityPack.HtmlNode objNewsDataNode in objNewsDataNodeCollection)\n    {\n        HtmlAgilityPack.HtmlNode objNewsTitleNode = objNewsDataNode.SelectSingleNode(\"span\");\n        string strTitle = objNewsTitleNode.InnerText;\n\n        \/\/ \u5c5e\u6027\u306e\u53d6\u5f97\n        string strURL = objNewsDataNode.GetAttributeValue(\"href\", \"\");\n\n        Console.WriteLine(strTitle + \":\" + strURL);\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u30ce\u30fc\u30c9\u306e\u6307\u5b9a\u306f<a href=\"https:\/\/msdn.microsoft.com\/ja-jp\/library\/ms256086(v=vs.120).aspx\" target=\"_blank\" rel=\"noreferrer noopener\">XPath<\/a>\u3092\u7528\u3044\u308b\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5168\u3066\u306e\u30ce\u30fc\u30c9\u304b\u3089\u62bd\u51fa\uff1a\/\/&lt;\u30bf\u30b0\u540d&gt;<\/li><li>\u6307\u5b9a\u3057\u305f\u30ce\u30fc\u30c9\u914d\u4e0b\u306b\u3042\u308b\u5168\u3066\u306e\u30ce\u30fc\u30c9\u304b\u3089\u62bd\u51fa\uff1a*\/\/&lt;\u30bf\u30b0\u540d&gt;<\/li><li>\u5c5e\u6027\u306e\u5024\u3092\u6307\u5b9a\u3057\u3066\u62bd\u51fa\uff1a&lt;\u30bf\u30b0\u540d&gt;[@&lt;\u5c5e\u6027\u540d&gt;='&lt;\u5c5e\u6027\u5024&gt;&#8217;]<\/li><li>\u6307\u5b9a\u3057\u305f\u5c5e\u6027\u306e\u5024\u3092\u542b\u3080\u3082\u306e\u3092\u62bd\u51fa\uff1a&lt;\u30bf\u30b0\u540d&gt;[contains(@&lt;\u5c5e\u6027\u540d&gt;, &#8216;&lt;\u5c5e\u6027\u5024&gt;&#8217;)]<\/li><li>\u6307\u5b9a\u3057\u305f\u5c5e\u6027\u306e\u5024\u3067\u59cb\u307e\u308b\u3082\u306e\u3092\u62bd\u51fa\uff1a&lt;\u30bf\u30b0\u540d&gt;[starts-with(@&lt;\u5c5e\u6027\u540d&gt;, &#8216;&lt;\u5c5e\u6027\u5024&gt;&#8217;)]<\/li><li>\u30c6\u30ad\u30b9\u30c8\u306b\u6307\u5b9a\u3057\u305f\u5024\u304c\u542b\u307e\u308c\u308b\u3082\u306e\u3092\u62bd\u51fa\uff1a&lt;\u30bf\u30b0\u540d&gt;[contains(., &#8216;&lt;\u5024&gt;&#8217;)]<\/li><li>\u30bf\u30b0\u306e\u5024\u3092\u53d6\u5f97\uff1aInnerHtml \/ InnerText \/ OuterHtml<\/li><li>\u30bf\u30b0\u306e\u5c5e\u6027\u3092\u53d6\u5f97\uff1aGetAttributeValue(&#8220;&lt;\u5c5e\u6027\u540d&gt;&#8221;, &#8220;&lt;\u521d\u671f\u5024&gt;&#8221;)<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Html Agility Pack \u3092\u4f7f\u7528\u3059\u308b\u3068\u3001HTML\u30bf\u30b0\u304c\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u3088\u3046\u306b\u6271\u3048\u3001\u7c21\u5358\u306b\u89e3\u6790\u3092\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u308b\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,15],"tags":[],"class_list":["post-367","post","type-post","status-publish","format-standard","hentry","category-c","category-15"],"_links":{"self":[{"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/367","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=367"}],"version-history":[{"count":5,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/367\/revisions"}],"predecessor-version":[{"id":730,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/367\/revisions\/730"}],"wp:attachment":[{"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=367"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}