0

我可以使用这个正则表达式读取和下载页面上的 .jpg 文件列表

MatchCollection match = Regex.Matches(htmlText,@"http://.*?\b.jpg\b", RegexOptions.RightToLeft);

输出示例: 来自html中这一行 的http://somefiles.jpg问题:我怎样才能读取这种格式的文件?<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

4 回答 4

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

于 2013-05-23T06:10:42.660 回答
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
于 2013-05-23T06:42:32.653 回答
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

于 2013-05-23T06:16:12.827 回答
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 内。

于 2013-05-23T06:11:18.017 回答