查找字符串列表是否多次包含相同元素
我正在为产品销售网站编写自己的特定网络爬虫。由于它们的编码性质非常糟糕,我得到了指向同一页面的网址。
示例一
http://www.hizlial.com/bilgisayar/bilgisayar-bilesenleri/bilgisayar/yazicilar/samsung-scx-3200-tarayici-fotokopi-lazer-yazici_30.033.1271.0043.htm
例如上面的页面与下面的页面相同
http://www.hizlial.com/bilgisayar-bilesenleri/bilgisayar/yazicilar/samsung-scx-3200-tarayici-fotokopi-lazer-yazici_30.033.1271.0043.htm
正如你所看到的,当你通过'/'
字符分割时它包含2个“bilgisayar”元素
所以我想要的是我想像这样分割网址
string[] lstSPlit = srURL.Split('/');
之后检查该列表是否多次包含相同的元素。任何元素。如果包含任何元素,我将跳过该 url,因为我已经从其他页面提取了真实的 url。那么最好的方法是什么?
更长但有效的版本
string[] lstSPlit = srHref.Split('/');
bool blDoNotAdd = false;
HashSet<string> splitHashSet=new HashSet<string>();
foreach (var vrLstValue in lstSPlit)
{
if (vrLstValue.Length > 1)
{
if (splitHashSet.Contains(vrLstValue) == false)
{
splitHashSet.Add(vrLstValue);
}
else
{
blDoNotAdd = true;
break;
}
}
}
I am writing my own specific web crawler for product selling websites. Due to their very bad coding nature i get with getting urls pointing same page.
Example one
http://www.hizlial.com/bilgisayar/bilgisayar-bilesenleri/bilgisayar/yazicilar/samsung-scx-3200-tarayici-fotokopi-lazer-yazici_30.033.1271.0043.htm
For example the page above is same as below
http://www.hizlial.com/bilgisayar-bilesenleri/bilgisayar/yazicilar/samsung-scx-3200-tarayici-fotokopi-lazer-yazici_30.033.1271.0043.htm
As you can see it contains 2 "bilgisayar" element when you split via '/'
character
So what i want is i want to split urls like this
string[] lstSPlit = srURL.Split('/');
After that check that whether that list contains same element more than once or not. Any element. If contains any element i will skip the url because i would have already have the real url extracted from some other page. So what is the best way of doing this ?
Longer but working version
string[] lstSPlit = srHref.Split('/');
bool blDoNotAdd = false;
HashSet<string> splitHashSet=new HashSet<string>();
foreach (var vrLstValue in lstSPlit)
{
if (vrLstValue.Length > 1)
{
if (splitHashSet.Contains(vrLstValue) == false)
{
splitHashSet.Add(vrLstValue);
}
else
{
blDoNotAdd = true;
break;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该比分组更快。 (我没有测量)
您可以通过编写自己的扩展方法来使其更快,该方法将项目添加到
HashSet
并在Add()
时立即返回 false返回假。你甚至可以使用邪恶的速记来做到这一点:
This ought to be faster than grouping. (I haven't measured)
You can make it even faster by writing your own extension method that adds items to a
HashSet<T>
and returns false immediately ifAdd()
returns false.You can even do that using a wicked shorthand: