どのように私は、ウェブサイトからのすべての画像を取得するために、HTMLの敏捷性パックを使用することができますか?

StackOverflow https://stackoverflow.com/questions/2113924

  •  22-09-2019
  •  | 
  •  

質問

私はちょうどHTMLAgilityPackとドキュメントはどんな例がありませんダウンロードします。

私は、ウェブサイトからのすべての画像をダウンロードする方法を探しています。アドレス文字列ではなく、物理的なイメージます。

<img src="blabalbalbal.jpeg" />

私は、それぞれのimgタグのソースを引っ張っする必要があります。私はちょうどライブラリと何が提供できるの感触を取得したいです。誰もが、これは仕事に最適なツールであると言いました。

編集

public void GetAllImages()
    {
        WebClient x = new WebClient();
        string source = x.DownloadString(@"http://www.google.com");

        HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
        document.Load(source);

                         //I can't use the Descendants method. It doesn't appear.
        var ImageURLS = document.desc
                   .Select(e => e.GetAttributeValue("src", null))
                   .Where(s => !String.IsNullOrEmpty(s));        
    }
役に立ちましたか?

解決

あなたはこのように、この使用してLINQを行うことができます

var document = new HtmlWeb().Load(url);
var urls = document.DocumentNode.Descendants("img")
                                .Select(e => e.GetAttributeValue("src", null))
                                .Where(s => !String.IsNullOrEmpty(s));

編集:このコードは、現在、実際に動作します。私は書き込みdocument.DocumentNodeに忘れていた。

他のヒント

その一例に基づいて

が、変更のXPathと

 HtmlDocument doc = new HtmlDocument();
 List<string> image_links = new List<string>();
 doc.Load("file.htm");
 foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//img"))
 {
    image_links.Add( link.GetAttributeValue("src", "") );
 }
私は、私は確信してどのようにどこかに配列を書き出すためにはないよので、この拡張機能を知りませんが、それは、少なくとも、あなたのデータを取得します。 (また、私が正しく、私は確信している配列を定義しません。申し訳ありません)。

編集

あなたの例を使用します:

public void GetAllImages()
    {
        WebClient x = new WebClient();
        string source = x.DownloadString(@"http://www.google.com");

        HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
        List<string> image_links = new List<string>();
        document.Load(source);

        foreach(HtmlNode link in document.DocumentElement.SelectNodes("//img"))
        {
          image_links.Add( link.GetAttributeValue("src", "") );
       }


    }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top