【问题标题】:search for hyperlinks in String在字符串中搜索超链接
【发布时间】:2021-02-05 21:26:39
【问题描述】:

我想搜索包含特定关键字的大字符串中的所有超链接。 超链接应包含以下关键字:制造商名称(例如三星)和下载。

String的结构如下:

<h2><a href="https://www.samsung.com/semiconductor/minisite/ssd/product/consumer/magician/" h="ID=SERP,5141.1">
Samsung Magician Software | Samsung V-NAND …</a></h2><div class="b_suffix b_secondaryText nowrap">
<a href="http://www.microsofttranslator.com/bv.aspx?ref=SERP&amp;br=ro&amp;mkt=nl-NL&amp;dl=nl&amp;lp=EN_NL&amp
;a=https%3a%2f%2fwww.samsung.com%2fsemiconductor%2fminisite%2fssd%2fproduct%2fconsumer%2fmagician%2f" h="ID=SERP
,5148.1">Deze pagina vertalen</a></div></div><div class="b_caption"><div class="b_attribution b_nav" u="0N|5131|
4627393552323160|A_CGcDrlDvI4S9DANesILYWPEhC2j7ly"><cite>https://www.<strong>samsung</strong>.com/.../minisite/
ssd/product/consumer/<strong>magician</strong></cite><span class="c_tlbxTrg"><span class="c_tlbxH" H="BASE:CACHE
DPAGEDEFAULT" K="SERP,5142.1"></span></span></div><p><strong>Samsung Magician</strong> software is designed to 
help you manage your <strong>Samsung</strong> SSD with a simple, intuitive user interface. <strong>Download
/strong> files &amp; find supported models.</p></div><div Class="dlCollapsedCnt"><div class="b_vlist2col b_deep"
><ul><li><h3 class="deeplink_title"><a href="https://www.samsung.com/semiconductor/minisite/ssd/product/consumer
/860evo" h="ID=SERP,5345.1">860 EVO</a></h3><p>The SSD to trust. The newest edition to the world’s best-selling*
SATA SSD series, the …</p></li></ul><ul></ul></div></div><form class="sc_rf dlsbox b_externalSearch b_divsec 
dlCollapsed" name="sc_rf dlsbox b_externalSearch b_divsec dlCollapsed5349" onsubmit="sa_ResubmitForm.Resubmit
(this, 'http:\/\/www.samsung.com\/nl\/function\/search\/espsearchResult?keywords=&amp;input_keyword=%query');
return false;"><input id="sc_rf dlsbox b_externalSearch b_divsec dlCollapsed5349_si" type="hidden" value="SERP,
5349.1"/> <input class="b_hide" value="ctxtb" id="h_c0" name="h_c0" /><input type="text" id="c0" name="query" 
maxlength="100" style="width:466px" class="ctxt" qi="1" data-gt="" placeholder="Zoeken in samsung.com" onfocus="
sj_evt.fire('LogTextBoxFocus', 'SERP','5515.1')" />  <span id="3C65B3_3_btn" class="cbtn" data-wire="I;button_
init;; |" data-appns="SERP" data-k="5517.1"><input type="submit" name="submit" id="sb_submit" value="Zoeken" 
style="width:100px" /></span></form></li><li class="b_algo"><div class="b_title"><h2>
<a href="https://www.samsung.com/semiconductor/minisite/ssd/download/tools/" h="ID=SERP,5156.1">

实际上只观察到第一个超链接,并且缺少关键字“下载”。所以我的输出是超链接https://www.samsung.com/semiconductor/minisite/ssd/product/consumer/magician/ 而不是https://www.samsung.com/semiconductor/minisite/ssd/download/tools/

这是我的实际代码:

String strStart = "<a href=\"";
String strEnd = "\" h=\"ID=";         
            
if (stringsource.Contains(strStart) && 
    stringsource.Contains(strEnd) && 
    stringsource.Contains(manufacture))
{
    int Start, End;
    Start = stringsource.IndexOf(strStart, 0)+strStart.Length;
    End = stringsource.IndexOf(strEnd, Start);

    Console.WriteLine("Text: " + stringsource.Substring(Start, End - Start));
    String substring = stringsource.Substring(Start, End -  Start);               
}

【问题讨论】:

  • 这是学习正则表达式的好时机……
  • 您只想要来自 hrefs 的链接还是来自表单的 onsubmit 功能的链接?
  • 您的字符串是 HTML。所以,请使用HTML parser
  • 您也没有检查"download" 字符串是否存在于stringsource.Substring(Start, End - Start)

标签: c# string hyperlink


【解决方案1】:

一个问题是您在找到第一个匹配项后不会继续搜索。第二个问题是您没有检查字符串是否包含“下载”一词。

您可以使用循环来解决这些问题,我们会从上次搜索停止的地方继续搜索。然后,我们还在结果中添加了“下载”检查。如果我们找到它,那么我们可以立即返回它,或者将它添加到一个列表中,如果我们可能期望多个结果(这就是我在下面所做的),则返回该列表:

public static List<string> GetManufacturerDownloadLinks(string input, string manufacturer)
{
    var result = new List<string>();
    if (string.IsNullOrEmpty(input)) return result;

    var strStart = "<a href=\"";
    var strEnd = "\" h=\"ID=";

    // Find the first index of the start string
    var start = input.IndexOf(strStart);

    while (start > -1 && start < input.Length - strEnd.Length)
    {
        // Find the first index of the end string after the start string
        var end = input.IndexOf(strEnd, start + strStart.Length);
        if (end < 0) break;

        // Get the substring between start and end
        var substring = input.Substring(start, end - start);

        // If it contains the required terms, add it to our list
        if (substring.Contains(manufacturer) && substring.Contains("download"))
            result.Add(input.Substring(start, end - start));

        // Find the next index of the start string after end
        start = input.IndexOf(strStart, end + strEnd.Length);
    }

    return result;
}

【讨论】:

    【解决方案2】:

    字符串中似乎只有 1 个带有“Samsung”和“download”字样的超链接。 即https://www.samsung.com/semiconductor/minisite/ssd/download/tools/

    解决此问题的最佳方法是获取字符串中的所有超链接并检查每个超链接中的制造商名称(在本例中为“三星”)和“下载”。

    检查此link 以获取 HTML 代码中的超链接。这可以很容易地使用正则表达式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-08
      • 1970-01-01
      • 2018-11-26
      • 2021-11-01
      • 2016-06-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多