LoginSignup
0
0

More than 1 year has passed since last update.

C#でスクレイピングする。Curlコマンドでhtmlを取得し、AngleSharpを利用してparseする。

Posted at

これは何

C#で、スクレイピングする例

注意書き

HTMLの取得は、Curlコマンドを利用している。
HTMLのParseは、AngleSharpを利用している。

AngleSharpのversionは0.11.0、
System.Text.Encoding.CodePagesのversionは4.3.0を利用している。
(AngleSharpと、CodePagesの、相性でエラーが出る模様で、他の方のページで、稼働実績のあったこの組み合わせを利用している。)

nugetを利用せず、DLLを取得して、利用している。
(nugetのファイルをzipとして解凍し、ファイルを取得している。)
DLLは、net46のフォルダ内のものを利用している。

実行コマンド例

ore_scrape.exe

コンパイル例

c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /reference:AngleSharp.dll;System.Text.Encoding.CodePages.dll; /target:exe ore_scrape.cs

ソース

ore_scrape.cs

//c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /reference:AngleSharp.dll;System.Text.Encoding.CodePages.dll; /target:exe ore_scrape.cs

using System;
using System.IO;
using System.Diagnostics;
using AngleSharp.Html.Parser;

public class ore_scrape
{

    ////Main
    public static void Main(string[] args){
        path = System.Windows.Forms.Application.StartupPath + "/";
        string local_url = path + "temp.html";
        my_curl("https://www.yahoo.co.jp",local_url);
        my_parse(local_url);
    }///Main

    ///--==--==--==
    private static void my_curl(string p1,string p2){
        ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe"); 
        string p0 = "/c curl " + p1 + " > " + p2;
        processStartInfo.Arguments = p0;
        //processStartInfo.CreateNoWindow = true;
        processStartInfo.UseShellExecute = true;
        Process process = Process.Start(processStartInfo); 
        process.WaitForExit();
        process.Close();
        return;
    }//--==--==--==

    ////--------------------
    private static void my_parse(string parse_url){
        var html = File.ReadAllText(parse_url, System.Text.Encoding.UTF8);
        var parser = new HtmlParser();
        var doc = parser.ParseDocument(html);
        var classpList = doc.GetElementsByTagName("h1");
        foreach (var c in classpList)
        {
            string t;
            t = c.TextContent;
            t = t.Trim();
            Console.WriteLine(t);
            File.AppendAllText(@".\temp.txt", t + Environment.NewLine);
        }
    }///--------------------

}

実行例

※www.yahoo.co.jpのh1タグを取得している

※取得対象の画面例
img1.png

※取得した結果の例

img2.png

参考サイト

nuget

https://www.nuget.org/packages/AngleSharp
https://www.nuget.org/packages/System.Text.Encoding.CodePages/

https://www.nuget.org/api/v2/package/AngleSharp/0.11.0
https://www.nuget.org/api/v2/package/System.Text.Encoding.CodePages/4.3.0

AngleSharp

2020-01-31
C#でAngleSharpを使ってHTMLをパースする
https://emotionwave.hatenablog.com/entry/2020/01/31/100000

2019.4.24
[C#] AngleSharpでHtmlParserの生成をするとFileNotFoundExceptionの例外が出る問題
https://d-vecter.blogspot.com/2019/04/c-anglesharphtmlparserfilenotfoundexcep.html

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0