C# 通过HtmlAgility&;自定义网络客户端 private void按钮1\u单击(对象发送者,事件参数e) { test(); } 公开无效测试() { Dictionary LnksDict=新字典(); 使用(SmartWebClient smwc=新的SmartWebClient()) { HtmlAgilityPack.HtmlDocument htmlDoc=新的HtmlAgilityPack.HtmlDocument(); htmlDoc.LoadHtml(smwc.DownloadString(“http://www.google.com/adplanner/static/top1000/")); var links=htmlDoc.DocumentNode .子体(“a”)。选择(x=>x.Attributes[“href”]); foreach(htmlDoc.DocumentNode.SelectNodes(“//a”)中的var链接) { var UrlVal=link.Attributes[“href”].Value; var name=UrlVal.Split('.')[1]; LnksDict.Add(名称,UrlVal); } } } #区域 公共类SmartWebClient:WebClient { 私有只读int-maxConcurentConnectionCount; 公共SmartWebClient(int-MaxConcurrentConnectionCount=20) { this.maxConcurentConnectionCount=maxConcurentConnectionCount; } 受保护的覆盖WebRequest GetWebRequest(Uri地址) { var httpWebRequest=(httpWebRequest)base.GetWebRequest(address); if(httpWebRequest==null) { 返回null; } 如果(MaxConcurrentConnectionCount!=0) { this.Proxy=null; this.Encoding=Encoding.GetEncoding(“UTF-8”); httpWebRequest.ServicePoint.ConnectionLimit=maxConcurentConnectionCount; } 返回httpWebRequest; } } #端区
在这段代码中,我试图构建一个URL列表,稍后用作自动完成源代码 我做错的不是将解析后的值添加到字典中 我需要找到一种方法添加域名作为密钥,即使已经存在 因此,我希望能够提出一个条件: 如果此词典中的C# 通过HtmlAgility&;自定义网络客户端 private void按钮1\u单击(对象发送者,事件参数e) { test(); } 公开无效测试() { Dictionary LnksDict=新字典(); 使用(SmartWebClient smwc=新的SmartWebClient()) { HtmlAgilityPack.HtmlDocument htmlDoc=新的HtmlAgilityPack.HtmlDocument(); htmlDoc.LoadHtml(smwc.DownloadString(“http://www.google.com/adplanner/static/top1000/")); var links=htmlDoc.DocumentNode .子体(“a”)。选择(x=>x.Attributes[“href”]); foreach(htmlDoc.DocumentNode.SelectNodes(“//a”)中的var链接) { var UrlVal=link.Attributes[“href”].Value; var name=UrlVal.Split('.')[1]; LnksDict.Add(名称,UrlVal); } } } #区域 公共类SmartWebClient:WebClient { 私有只读int-maxConcurentConnectionCount; 公共SmartWebClient(int-MaxConcurrentConnectionCount=20) { this.maxConcurentConnectionCount=maxConcurentConnectionCount; } 受保护的覆盖WebRequest GetWebRequest(Uri地址) { var httpWebRequest=(httpWebRequest)base.GetWebRequest(address); if(httpWebRequest==null) { 返回null; } 如果(MaxConcurrentConnectionCount!=0) { this.Proxy=null; this.Encoding=Encoding.GetEncoding(“UTF-8”); httpWebRequest.ServicePoint.ConnectionLimit=maxConcurentConnectionCount; } 返回httpWebRequest; } } #端区,c#,.net,winforms,html-parsing,html-agility-pack,C#,.net,Winforms,Html Parsing,Html Agility Pack,在这段代码中,我试图构建一个URL列表,稍后用作自动完成源代码 我做错的不是将解析后的值添加到字典中 我需要找到一种方法添加域名作为密钥,即使已经存在 因此,我希望能够提出一个条件: 如果此词典中的键已存在,请将当前链接的集合索引添加到键的字符串。值作为后缀 或者如果你想一起提出一个不同的解决方案。。。我很高兴看到新的例子。 谢谢我想你想要的不是字典,而是字典。这样,您就可以为每个域构建URL列表。将项目添加到列表中的代码为: private void button1_Click(obj
键
已存在,请将当前链接的集合索引
添加到键的字符串。值
作为后缀
或者如果你想一起提出一个不同的解决方案。。。我很高兴看到新的例子。
谢谢我想你想要的不是字典
,而是字典
。这样,您就可以为每个域构建URL列表。将项目添加到列表中的代码为:
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
var UrlVal=link.Attributes[“href”].Value;
var name=UrlVal.Split('.')[1];
//获取此主机的链接
HashSet-hostLinksList;
如果(!LnksDict.TryGetValue(名称,out hostLinksList))
{
hostLinksList=newhashset();
LnksDict.Add(名称、主机链接列表);
}
//将URL添加到此主机的链接列表
hostLinksList.Add(UrlVal);
这里的关键是,当项目已经存在时,在哈希集
上调用Add
不会引发异常。它只是不再添加它,并返回false
,以指示该项已在集合中
完成后,您将拥有每个主机(域)的URL列表,然后可以使用该列表自动完成
顺便说一下,您使用Split('.')
拆分主机的方法不会很好地工作。它假定域的形式为“www.example.com”。例如,如果你遇到“example.com”(没有“www”),你会得到“com”作为名字。此外,“www.example.com”将与“www.example.org”和“www.example.co.uk”发生冲突。你需要一种更好的方法来识别主机。1为什么你现在还没睡着(:我醒着的时候就在这里。)(:,第二,我发现每完成一项我想获得结果的简单任务,我突然有5个新对象要研究(:(HashSet
),第三也是最后一点,我想感谢你提供了一个非常好的解释性答案!!希望在你有时间在我的帖子中“花时间”回答问题的时候也能在这里见到你。。
var UrlVal= link.Attributes["href"].Value;
var name = UrlVal.Split('.')[1];
// get links for this host
HashSet hostLinksList;
if (!LnksDict.TryGetValue(name, out hostLinksList))
{
hostLinksList = new HashSet<string>();
LnksDict.Add(name, hostLinksList);
}
// add the URL to the list of links for this host
hostLinksList.Add(UrlVal);