繁体   English   中英

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

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

我正在尝试获取图像的网址,目前我的代码可以正常工作,但需要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);
            }
        }
    }

我需要和平一些不需要webBrowser的东西,但是我真的不知道该怎么做。

另外, HtmlDocument htmlDocument向该方法提供HtmlDocument htmlDocument ,我还需要为它提供一个简单的string

还有其他选择吗?

尝试这样的事情:

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;
}

改编自: 如何使用HTML Agility Pack从网站检索所有图像?

编辑以包括用法,并向GetFileUrls()添加模式参数。

暂无
暂无

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

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