【问题标题】:Hashset handling to avoid stuck in loop during iteration哈希集处理以避免在迭代过程中陷入循环
【发布时间】:2017-02-11 11:39:26
【问题描述】:

我正在从事图像挖掘项目,我使用 Hashset 而不是数组来避免在收集 url 时添加重复的 url,我到达了代码点来迭代包含主要 url 的 Hashset,并且在迭代中我去并下载主 URL 的页面并将它们添加到 Hashet 中,然后继续,在迭代期间我应该排除每个扫描的 url,并排除(删除)每个以 jpg 结尾的 url,直到 url 计数的 Hashet达到 0,问题是我在这个迭代中面临无限循环,我可能会得到 url(我们称之为 X)

1- 我扫描 url X 的页面 2-获取页面X的所有网址(通过应用过滤器) 3- 使用 unioinwith 将 url 添加到 Hashset 4-删除扫描的url X

当其中一个 URL Y 被扫描时再次带 X 时,问题就出现了

我应该使用字典和密钥作为“扫描”吗?我将尝试在此处发布结果,抱歉,我发布问题后想到...

我设法为一个网址解决了这个问题,但似乎它发生在其他网址上以生成循环,所以即使在删除链接后如何处理哈希集以避免重复,,,我希望我的观点是清除。

while (URL_Can.Count != 0)
 {

                  tempURL = URL_Can.First();

                   if (tempURL.EndsWith("jpg")) 
                    {
                        URL_CanToSave.Add(tempURL);
                        URL_Can.Remove(tempURL);

                    }
                    else
                    {

                        if (ExtractUrlsfromLink(client, tempURL, filterlink1).Contains(toAvoidLoopinLinks))
                        {

                            URL_Can.Remove(tempURL);

                            URL_Can.Remove(toAvoidLoopinLinks);
                        }
                        else 
                        {
                            URL_Can.UnionWith(ExtractUrlsfromLink(client, tempURL, filterlink1));

                            URL_Can.UnionWith(ExtractUrlsfromLink(client, tempURL, filterlink2));

                            URL_Can.Remove(tempURL);

                            richTextBox2.PerformSafely(() => richTextBox2.AppendText(tempURL + "\n"));
                        }

                    }

                   toAvoidLoopinLinks = tempURL;

                }

【问题讨论】:

    标签: url hashset mining


    【解决方案1】:

    感谢大家,我设法使用 Dictionary 而不是 Hashset 解决了这个问题,并使用 Key 来保存 URL 和保存 int 的值,如果 url 被扫描,则为 1,如果 url 仍然存在,则为 0未处理,以下是我的代码。 我使用另一个 Dictionary "URL_CANtoSave" 来保存以 jpg "my target" 结尾的 url……而这个 While.. 循环可以循环,直到根据您在过滤器字符串变量中指定的值,网站的所有 url 都用完你相应地解析网址。

    所以要打破循环,您可以在 URL_CantoSave 中指定要获取的图片数量。

      return Task.Factory.StartNew(() =>
            {
                try
                {
    
    
                    string tempURL;
    
                    int i = 0;
    

    //我之前是设置Dictionary Key的值,1或者0(1代表扫描, 0 表示尚未迭代,直到扫描完所有字典键或根据您在其他字典中收集的图像 url 数量在中间中断

                   while (URL_Can.Values.Where(value => value.Equals(0)).Any())
    
    
                    {
    

    // 取出 1 个密钥并将其放入临时变量中

                        tempURL = URL_Can.ElementAt(i).Key;
    

    // 检查它是否以您的目标文件扩展名结尾。在这种情况下是图像文件

                       if (tempURL.EndsWith("jpg")) 
                        {
                            URL_CanToSave.Add(tempURL,0);
    
                            URL_Can.Remove(tempURL);
    
                        }
    

    //如果不是图片,根据url下载页面并继续分析

                        else
                        {
    

    //如果之前没有扫描过url

                            if (URL_Can[tempURL] != 1) 
                            {
    

    // 这里看起来有点复杂,其中 Add2Dic 是添加到字典而不再次添加 Key 的过程(解决主要问题!!) “ExtractURLfromLink”是另一个返回包含所有链接的字典的过程,通过下载 url 的文档字符串并对其进行分析, 您可以根据您的分析添加删除过滤器字符串

    Add2Dic(ExtractUrlsfromLink(client, tempURL, filterlink1), URL_Can, false);
    Add2Dic(ExtractUrlsfromLink(client, tempURL, filterlink2), URL_Can, false);
    
     URL_Can[tempURL] = 1;  //  to set it as scanned link
    
    
        richTextBox2.PerformSafely(() => richTextBox2.AppendText(tempURL + "\n"));
                            }
    
    
    
                        }
    
    
            statusStrip1.PerformSafely(() => toolStripProgressBar1.PerformStep());
    

    // 另一个技巧是让这个迭代继续进行,直到它扫描所有收集到的链接

                        i++;  if (i >= URL_Can.Count) { i = 0; }
    
                        if (URL_CanToSave.Count >= 150) { break; }
    
                    }
    
    
                    richTextBox2.PerformSafely(() => richTextBox2.Clear());
    
                    textBox1.PerformSafely(() => textBox1.Text = URL_Can.Count.ToString());
    
    
                    return ProcessCompleted = true;
    
    
    
    
                }
                catch (Exception aih)
                {
    
                    MessageBox.Show(aih.Message);
    
                    return ProcessCompleted = false;
    
                    throw;
                }
    
    
                {
                  richTextBox2.PerformSafely(()=>richTextBox2.AppendText(url+"\n"));
                }
            })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-19
      • 2019-09-14
      • 1970-01-01
      • 1970-01-01
      • 2012-09-10
      • 1970-01-01
      • 2012-09-16
      • 1970-01-01
      相关资源
      最近更新 更多