简体   繁体   English

在没有Web浏览器的情况下获取文件URL-C#

[英]Get a file url without webbrowser - C#

I'm trying to get the Url of an image, at the moment I have this code which does work but needs a webBrowser to do so. 我正在尝试获取图像的网址,目前我的代码可以正常工作,但需要webBrowser来执行。

    public void getFileUrl(HtmlDocument htmlDocument)
    {
        HtmlElementCollection htmlCollectionImage = htmlDocument.Images;
        foreach (HtmlElement htmlImage in htmlCollectionImage)
        {
            string Url = htmlImage.GetAttribute("src");
            if (Url.StartsWith("http://www.exemple.com/"))
            {
                MessageBox.Show(Url);
            }
        }
    }

I need to peace something up which doesn't require the webBrowser, but I really don't know how to do that. 我需要和平一些不需要webBrowser的东西,但是我真的不知道该怎么做。

Also instead of an HtmlDocument htmlDocument being fed to the method, I need to feed it a simple string . 另外, HtmlDocument htmlDocument向该方法提供HtmlDocument htmlDocument ,我还需要为它提供一个简单的string

Any alternative? 还有其他选择吗?

Try something like this: 尝试这样的事情:

static void Main()
{
    var fileUrls = GetFileUrl(@"https://stackoverflow.com/questions/34054662/get-a-file-url-without-webbrowser-c-sharp", @"https://www.gravatar.com/");

    foreach (string url in fileUrls)
    {
        Console.WriteLine(url);
    }

    Console.ReadKey();
}

public static IEnumerable<string> GetFileUrls(string url)
{
    var document = new HtmlWeb().Load(url);
    var urls = document.DocumentNode.Descendants("img")
                                    .Select(e => e.GetAttributeValue("src", null))
                                    .Where(s => s.ToLower().StartsWith(pattern));

    return urls;
}

Adapted from: How can I use HTML Agility Pack to retrieve all the images from a website? 改编自: 如何使用HTML Agility Pack从网站检索所有图像?

Edited to include usage and add a pattern parameter to GetFileUrls(). 编辑以包括用法,并向GetFileUrls()添加模式参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM