问题
I cant edit my code so you can delete this post
回答1:
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
var teams = doc.DocumentNode.SelectNodes("//td[@width='313']")
.Select(td => new TeamClass
{
TeamName = td.Element("a").InnerText,
TeamId = HttpUtility.ParseQueryString(td.Element("a").Attributes["href"].Value)["ItemTypeID"]
})
.ToList();
回答2:
Have a look at this lib http:HTML Agility Pack.
It helps you with HTML parsing.
回答3:
You can use Regular expression
String html; //your html string
String pattern = @"action=ViewItemDetails&ItemType[I|i]D=(\d*)"">(.*)</a>";
MatchCollection matches = Regex.Matches(html, pattern);
var list = new List<TeamClass>();
foreach (Match match in matches)
{
TeamClass team = new TeamClass();
team.TeamName = match.Groups[2].Value;
team.TeamId = Int32.Parse(match.Groups[1].Value);
list.Add(team);
}
回答4:
Try Html Agility:
try something like (Untested Code):
var TeamList = from lnks in document.DocumentNode.Descendants()
where lnks.Name == "a" &&
lnks.Attributes["href"] != null &&
lnks.InnerText.Trim().Length > 0
select new
{
TeamId= (lnks.Attributes["href"].Value).
Substring((lnks.Attributes["href"].Value).Length-1, 1),
TeamName= lnks.InnerText
};
来源:https://stackoverflow.com/questions/13194614/how-can-i-parse-html-string