所以我试图从论坛中获取成员个人资料链接并将它们显示在控制台应用程序中。我想要做的是从网页中获取所有链接并将它们打印出来。
当前我正在获取这样的页面源:
String source = WebClient.DownloadString("URL");
我想要做的是遍历该字符串并找到这样的每个字符串:
<h3 class='ipsType_subtitle'>
<strong><a href='http://www.website.org/community/user/8416-unreal/' title='View Profile'>!Unreal</a></strong>
</h3>
然后,一旦我得到那部分,我想像这样获得网址:
http://www.website.org/community/user/8416-unreal/
当前这是我尝试过的代码,它可以工作。但只抓取其中一个链接:
WebClient c = new WebClient();
String members = c.DownloadString("http://www.powerbot.org/community/members/");
int times = Regex.Matches(members, "<h3 class='ipsType_subtitle'>").Count;
Console.WriteLine(times.ToString());
for (int i = 1; i < times; i++)
{
try
{
int start = members.IndexOf("<h3 class='ipsType_subtitle'>");
members = members.Substring(start, 500);
String[] next = members.ToString().Split(new string[] { "a href='" }, StringSplitOptions.None);
String[] link = next[1].Split(' ');
Console.WriteLine(link[0].Replace("'", ""));
}
catch(Exception e) { Console.WriteLine("Failed: " + e.ToString()); }
}
Console.Read();
谢谢。