我有一个 html 页面,其链接如/with_us.php?page=digit 和 out.php?i=digit 。我如何从页面获取所有这些链接,但如果我可以立即仅收集此链接中的数字,那就更好了
最佳答案
HTML Agility Pack是理想的选择;这与主页上的示例几乎相同:
foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[@href]")
{
string href = link["href"].Value;
}
现在只需解析“href”;也许是这样的:
Match match = Regex.Match(href, @"[&?]\w+=(\d+)");
int i;
if (match.Success && int.TryParse(match.Groups[1].Value, out i))
{
Console.WriteLine(i);
}
关于c# - 正则表达式 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1339335/