private void button1_Click(object sender, EventArgs e)
{
test();
}
public void test()
{
Dictionary<string, string> LnksDict = new Dictionary<string, string>();
using (SmartWebClient smwc = new SmartWebClient())
{
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.LoadHtml(smwc.DownloadString("http://www.google.com/adplanner/static/top1000/"));
var links = htmlDoc.DocumentNode
.Descendants("a").Select(x => x.Attributes["href"]);
foreach (var link in htmlDoc.DocumentNode.SelectNodes("//a"))
{
var UrlVal= link.Attributes["href"].Value;
var name = UrlVal.Split('.')[1];
LnksDict.Add(name, UrlVal);
}
}
}
#region <<=========== SmWbCl ============>>
public class SmartWebClient : WebClient
{
private readonly int maxConcurentConnectionCount;
public SmartWebClient(int maxConcurentConnectionCount = 20)
{
this.maxConcurentConnectionCount = maxConcurentConnectionCount;
}
protected override WebRequest GetWebRequest(Uri address)
{
var httpWebRequest = (HttpWebRequest)base.GetWebRequest(address);
if (httpWebRequest == null)
{
return null;
}
if (maxConcurentConnectionCount != 0)
{
this.Proxy = null;
this.Encoding = Encoding.GetEncoding("UTF-8");
httpWebRequest.ServicePoint.ConnectionLimit = maxConcurentConnectionCount;
}
return httpWebRequest;
}
}
#endregion
在这段代码中,我试图构建一个 url 列表,以便稍后用作自动完成源。
我做错的是将解析的值添加到字典中的条件。
我需要找到一种方法来添加域名作为密钥,即使已经存在,
所以我希望能够提出一个条件:
如果key
此字典中的已经存在,则添加collection index
当前link
到string.value
的key
作为后缀
或者,如果您想一起提出不同的解决方案......我很乐意看到新的例子。谢谢