在ASP.NET C#中读取正则表达式

时间:2013-05-23 06:01:27

标签: c# asp.net regex

我可以使用这个正则表达式

在页面上阅读和下载.jpg文件列表
MatchCollection match = Regex.Matches(htmlText,@"http://.*?\b.jpg\b", RegexOptions.RightToLeft);

此行的输出示例:http://somefiles.jpg html中的<img src="http://somefiles.jpg"/>


问题:我怎样才能读取这种格式的文件?

<a href="download/datavoila-setup.exe" id="button_download" title="Download your copy of DataVoila!" onclick="pageTracker._trackPageview('/download/datavoila-setup.exe')"></a>

我只想在页面上使用.exe提取文件。所以在上面的示例中^我只想获取datavoila-setup.exe文件。对不起,我是一个小菜鸟,并混淆了怎么做T_T。提前感谢任何可以帮助我的人。 :)

这是我更新的代码,但我在HtmlDocument上遇到错误doc = new HtmlDocument();部分“无源可用”,我得到列表的空值:(

 protected void Button2_Click(object sender, EventArgs e)
        {
            //Get the url given by the user
            string urls;
            urls = txtSiteAddress.Text;
            StringBuilder result = new StringBuilder();

            //Give request to the url given 
            HttpWebRequest requesters = (HttpWebRequest)HttpWebRequest.Create(urls);
            requesters.UserAgent = "";

            //Check for the web response
            WebResponse response = requesters.GetResponse();
            Stream streams = response.GetResponseStream();

            //reads the url as html codes
            StreamReader readers = new StreamReader(streams);
            string htmlTexts = readers.ReadToEnd();

            HtmlDocument doc = new HtmlDocument();
            doc.Load(streams);
            var list = doc.DocumentNode.SelectNodes("//a[@href]")
                         .Select(p => p.Attributes["href"].Value)
                         .Where(x => x.EndsWith("exe"))
                         .ToList();
           doc.Save("list");
           }

这是Flipbed回答它有效,但不是我没有得到一个干净的捕获:(我认为有一些东西要编辑将HTML拆分为文本

protected void Button2_Click(object sender, EventArgs e)
        {
            //Get the url given by the user
            string urls;
            urls = txtSiteAddress.Text;
            StringBuilder result = new StringBuilder();

            //Give request to the url given 
            HttpWebRequest requesters = (HttpWebRequest)HttpWebRequest.Create(urls);
            requesters.UserAgent = "";

            //Check for the web response
            WebResponse response = requesters.GetResponse();
            Stream streams = response.GetResponseStream();

            //reads the url as html codes
            StreamReader readers = new StreamReader(streams);
            string htmlTexts = readers.ReadToEnd();

            WebClient webclient = new WebClient();
            string checkurl = webclient.DownloadString(urls);

            List<string> list = new List<string>();//!3

            //Splits the html into with \ into texts
            string[] parts = htmlTexts.Split(new string[] { "\"" },//!3
             StringSplitOptions.RemoveEmptyEntries);//!3

            //Compares the split text with valid file extension
            foreach (string part in parts)//!3
            {
                if (part.EndsWith(".exe"))//!3
                {
                    list.Add(part);//!3

                    //Download the data into a Byte array
                    byte[] fileData = webclient.DownloadData(this.txtSiteAddress.Text + '/' + part);//!6

                    //Create FileStream that will write the byte array to
                    FileStream file =//!6
                            File.Create(this.txtDownloadPath.Text + "\\" + list);//!6

                    //Write the full byte array to the file
                    file.Write(fileData, 0, fileData.Length);//!6

                    //Download message complete
                    lblMessage.Text = "Download Complete!";

                    //Clears the textfields content
                    txtSiteAddress.Text = "";
                    txtDownloadPath.Text = "";

                    //Close the file so other processes can access it
                    file.Close();
                    break;
                }

            }

4 个答案:

答案 0 :(得分:3)

正则表达式不是解析HTML文件的好选择..

HTML不严格,格式也不规则..

使用htmlagilitypack

您可以使用此代码使用HtmlAgilityPack

检索所有exe
HtmlWeb web = new HtmlWeb();
HtmlDocument doc = web.Load("http://yourWebSite.com");

var itemList = doc.DocumentNode.SelectNodes("//a[@href]")//get all hrefs
                  .Select(p => p.Attributes["href"].Value)
                  .Where(x=>x.EndsWith("exe"))
                  .ToList();

itemList现在包含所有exe的

答案 1 :(得分:3)

这不是一个答案,但对评论来说太长了。 (我稍后会删除它)

要解决问题它可行,它不起作用;一个完整的代码,适合那些可能想要检查的人

string html = @"<a href=""download/datavoila-setup.exe"" id=""button_download"" title=""Download your copy of DataVoila!"" onclick=""pageTracker._trackPageview('/download/datavoila-setup.exe')""></a>";
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);


//Anirudh's Solution
var itemList = doc.DocumentNode.SelectNodes("//a//@href")//get all hrefs
                .Select(p => p.InnerText)
                .Where(x => x.EndsWith("exe"))
                .ToList();
//returns empty list 


//correct one      
var itemList2 = doc.DocumentNode.SelectNodes("//a[@href]") 
                 .Select(p => p.Attributes["href"].Value)
                 .Where(x => x.EndsWith("exe"))
                 .ToList();
 //returns download/datavoila-setup.exe

答案 2 :(得分:1)

我会使用FizzlerEx,它将类似jQuery的语法添加到HTMLAgilityPack中。使用ends-with选择器测试href属性:

using HtmlAgilityPack;
using Fizzler.Systems.HtmlAgilityPack;

var web = new HtmlWeb();
var document = web.Load("http://example.com/page.html")
var page = document.DocumentNode;

foreach(var item in page.QuerySelectorAll("a[href$='exe']"))
{
    var file = item.Attributes["href"].Value;
}

解释为什么用RegEx解析HTML是不好的:http://www.codinghorror.com/blog/2009/11/parsing-html-the-cthulhu-way.html

答案 3 :(得分:0)

您可以使用普通代码,而不是使用正则表达式。

        List<string> files = new List<string>();
        string[] parts = htmlText.Split(new string[]{"\""},                
             StringSplitOptions.RemoveEmptyEntries);
        foreach (string part in parts)
        {
            if (part.EndsWith(".exe"))
                files.Add(part);
        }

在这种情况下,您将在文件列表中找到所有找到的文件。

编辑:

你可以这样做:

List<string> files = new List<string>();
string[] hrefs = htmlText.Split(new string[]{"href=\""},                
     StringSplitOptions.RemoveEmptyEntries);
foreach (string href in hrefs)
{
     string[] possibleFile = href.Split(new string[]{"\""}, 
           StringSplitOptions.RemoveEmptyEntries);
     if (possibleFile.Length() > 0 && possibleFile[0].EndsWith(".exe"))
         files.Add(possibleFile[0]);
}

这也会检查exe文件是否在href中。