2

今日、私のコードでは、次のような Web サイトから画像をダウンロードしています。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HtmlAgilityPack;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using System.Net;
using System.Web;
using System.Threading;
using DannyGeneral;
using GatherLinks;

namespace GatherLinks
{
    class RetrieveWebContent
    {
        HtmlAgilityPack.HtmlDocument doc;
        string imgg;
        int images;

        public RetrieveWebContent()
        {
            images = 0;
        }

        public List<string> retrieveImages(string address)
        {
            try
            {
                doc = new HtmlAgilityPack.HtmlDocument();
                System.Net.WebClient wc = new System.Net.WebClient();
                List<string> imgList = new List<string>();
                doc.Load(wc.OpenRead(address));
                HtmlNodeCollection imgs = doc.DocumentNode.SelectNodes("//img[@src]");
                if (imgs == null) return new List<string>();

                foreach (HtmlNode img in imgs)
                {
                    if (img.Attributes["src"] == null)
                        continue;
                    HtmlAttribute src = img.Attributes["src"];

                    imgList.Add(src.Value);
                    if (src.Value.StartsWith("http") || src.Value.StartsWith("https") || src.Value.StartsWith("www"))
                    {
                        images++;
                        string[] arr = src.Value.Split('/');
                        imgg = arr[arr.Length - 1];
                        wc.DownloadFile(src.Value, @"d:\MyImages\" + imgg);
                    }
                }

                return imgList;
            }
            catch
            {
                Logger.Write("There Was Problem Downloading The Image: " + imgg);
                return null;  
            }
        }
    }
}

しかし、多くの場合、画像は Java スクリプトの背後または下にあり、定期的にダウンロードすることはできません。画像および/または画像やすべてを含む完全なWebサイトコンテンツ全体を取得/ダウンロードするにはどうすればよいですか?後でハードディスクにすべてのコンテンツツリーを含む完全なWebサイトを作成して、オフラインでサーフィンできるようにします.

4

1 に答える 1

0

実際のブラウザを使用して、そこから画像を保存します.Rubyでのソリューションについては、Watir Webdriver をご覧ください。このライブラリは、ブラウザを自動化するのに役立ちます... Nokogiriと組み合わせて使用​​して、上記の目的を達成します..

Python の同等物も存在します。

Webdriver はまだ保存機能をサポートしていませんが、古い「Watir」はサポートしています。また、Javascript 言語でブラウザーの自動化を提供するCasperJSを調べることもできます。

于 2012-10-07T11:13:59.303 に答える