在ASP.NET C#中读取正则表达式
本文关键字:读取 正则表达式 ASP NET | 更新日期: 2023-09-27 18:14:52
我可以使用这个正则表达式读取并下载页面上的.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
文件。对不起,我有点笨,弄不清怎么做。提前感谢任何能帮助我的人。:(
这是我更新的代码,但我在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;
}
}
这不是一个答案,但对于注释来说太长了。(我稍后会删除(
为了解决问题它工作,它不工作等等;一个完整的代码,对于那些可能想检查的人
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
Regex不是解析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的
我会使用FizzleEx,它为HTMLAgilityPack添加了类似jQuery的语法。使用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
您可以不使用正则表达式,而只使用普通代码。
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中。