Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 带C语言的图像刮刀#_C#_.net - Fatal编程技术网

C# 带C语言的图像刮刀#

C# 带C语言的图像刮刀#,c#,.net,C#,.net,我正在浏览网页源代码,将添加到HtmlElementCollection。然后,我尝试使用foreach循环遍历元素集合中的每个元素,并通过url下载图像 这是我到目前为止所拥有的。我现在的问题是什么都没有下载,而且我认为我的元素没有通过标记名正确添加。如果他们是我似乎不能参考他们的下载 public partial class Form1 : Form { public Form1() { InitializeComponent(); } pu

我正在浏览网页源代码,将
添加到
HtmlElementCollection
。然后,我尝试使用foreach循环遍历元素集合中的每个元素,并通过url下载图像

这是我到目前为止所拥有的。我现在的问题是什么都没有下载,而且我认为我的元素没有通过标记名正确添加。如果他们是我似乎不能参考他们的下载

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    public void button1_Click(object sender, EventArgs e)
    {
        string url = urlTextBox.Text;
        string sourceCode = WorkerClass.ScreenScrape(url);
        StreamWriter sw = new StreamWriter("sourceScraped.html");
        sw.Write(sourceCode);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        string url = urlTextBox.Text;
        WebBrowser browser = new WebBrowser();
        browser.Navigate(url);
        HtmlElementCollection collection;
        List<HtmlElement> imgListString = new List<HtmlElement>();
        if (browser != null)
        {
            if (browser.Document != null)
            {
                collection = browser.Document.GetElementsByTagName("img");
                if (collection != null)
                {
                    foreach (HtmlElement element in collection)
                    {
                        WebClient wClient = new WebClient();
                        string urlDownload = element.FirstChild.GetAttribute("src");
                        wClient.DownloadFile(urlDownload, urlDownload.Substring(urlDownload.LastIndexOf('/')));
                    }
                }
            }
        }
    }
}
公共部分类表单1:表单
{
公共表格1()
{
初始化组件();
}
公共无效按钮1\u单击(对象发送者,事件参数e)
{
字符串url=urlTextBox.Text;
字符串sourceCode=WorkerClass.screensrap(url);
StreamWriter sw=新的StreamWriter(“sourceScraped.html”);
sw.Write(源代码);
}
私有无效按钮2\u单击(对象发送者,事件参数e)
{
字符串url=urlTextBox.Text;
WebBrowser browser=新的WebBrowser();
浏览器.导航(url);
HtmlElementCollection集合;
List imgListString=新列表();
如果(浏览器!=null)
{
如果(browser.Document!=null)
{
collection=browser.Document.GetElementsByTagName(“img”);
if(集合!=null)
{
foreach(集合中的HtmlElement元素)
{
WebClient wClient=新的WebClient();
字符串urlDownload=element.FirstChild.GetAttribute(“src”);
wClient.DownloadFile(urlDownload,urlDownload.Substring(urlDownload.LastIndexOf('/'));
}
}
}
}
}
}
}看看


您需要做的是下载并解析HTML,然后处理您感兴趣的元素。对于此类任务,它是一个很好的工具。

对于您称为“导航”的任务,您假定文档已准备好遍历和检查图像。但实际上,加载需要一些时间。您需要等待文档加载完成

将事件
DocumentCompleted
添加到浏览器对象

 browser.DocumentCompleted += browser_DocumentCompleted;
执行为

static void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    WebBrowser browser = (WebBrowser)sender;
    HtmlElementCollection collection;
    List<HtmlElement> imgListString = new List<HtmlElement>();
    if (browser != null)
    {
        if (browser.Document != null)
        {
            collection = browser.Document.GetElementsByTagName("img");
            if (collection != null)
            {
                foreach (HtmlElement element in collection)
                {
                    WebClient wClient = new WebClient();
                    string urlDownload = element.GetAttribute("src");
                    wClient.DownloadFile(urlDownload, urlDownload.Substring(urlDownload.LastIndexOf('/')));
                }
            }
        }
    }
}
static void browser\u DocumentCompleted(对象发送者,WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser浏览器=(WebBrowser)发送方;
HtmlElementCollection集合;
List imgListString=新列表();
如果(浏览器!=null)
{
如果(browser.Document!=null)
{
collection=browser.Document.GetElementsByTagName(“img”);
if(集合!=null)
{
foreach(集合中的HtmlElement元素)
{
WebClient wClient=新的WebClient();
字符串urlDownload=element.GetAttribute(“src”);
wClient.DownloadFile(urlDownload,urlDownload.Substring(urlDownload.LastIndexOf('/'));
}
}
}
}
}

对于任何感兴趣的人,这里是解决方案。这正是Damith说的。我发现Html敏捷包有点破损。这是我第一次尝试使用。这对我来说是一个更可行的解决方案,这是我的最终代码

private void button2_Click(object sender, EventArgs e)
    {
        string url = urlTextBox.Text;
        WebBrowser browser = new WebBrowser();
        browser.Navigate(url);
        browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DownloadFiles);
    }

    private void DownloadFiles(object sender, WebBrowserDocumentCompletedEventArgs e)
    {

        HtmlElementCollection collection;
        List<HtmlElement> imgListString = new List<HtmlElement>();

        if (browser != null)
        {
            if (browser.Document != null)
            {
                collection = browser.Document.GetElementsByTagName("img");
                if (collection != null)
                {
                    foreach (HtmlElement element in collection)
                    {
                        string urlDownload = element.GetAttribute("src");
                        if (urlDownload != null && urlDownload.Length != 0)
                        {
                            WebClient wClient = new WebClient();
                            wClient.DownloadFile(urlDownload, "C:\\users\\folder\\location\\" + urlDownload.Substring(urlDownload.LastIndexOf('/')));
                        }
                    }
                }
            }
        }
    }
}
private void按钮2\u单击(对象发送者,事件参数e)
{
字符串url=urlTextBox.Text;
WebBrowser browser=新的WebBrowser();
浏览器.导航(url);
browser.DocumentCompleted+=新的WebBrowserDocumentCompletedEventHandler(下载文件);
}
私有void下载文件(对象发送者,WebBrowserDocumentCompletedEventArgs e)
{
HtmlElementCollection集合;
List imgListString=新列表();
如果(浏览器!=null)
{
如果(browser.Document!=null)
{
collection=browser.Document.GetElementsByTagName(“img”);
if(集合!=null)
{
foreach(集合中的HtmlElement元素)
{
字符串urlDownload=element.GetAttribute(“src”);
if(urlDownload!=null&&urlDownload.Length!=0)
{
WebClient wClient=新的WebClient();
wClient.DownloadFile(urlDownload,“C:\\users\\folder\\location\\”+urlDownload.Substring(urlDownload.LastIndexOf('/'));
}
}
}
}
}
}
}

}

您正试图浏览网页并添加。。。什么?检查URL下载值以获得有效路径。这正是我所做的。成功了。我正要发布我自己的答案!哈哈,很高兴听到这个消息。请接受其中一个答案,或者您可以发布自己的答案,如果与此不同,请将其作为答案接受。对不起。我没有注意到有一个地方可以接受这个答案。我是新来的。