首页 > 解决方案 > 在源代码中查找链接并复制整个链接 webclient

问题描述

所以我正在使用c#,我想从页面中获取一个以设置域开头的链接。

示例我要获取的链接是https://example.com/yikes/######,其中 '#' 是随机字符串。

请注意,这是在一个包含多个链接的大型 html 文件中。一些链接还包含https://example.com/但没有包含https://example.com/yikes/但这个。

它也在 iFrame 中。我不知道这是否是有用的信息。或不。

html看起来像这样

<!DOCTYPE html>
<html>
<body>

<a href="https://example.com">This is a link</a>
<a href="https://example.com/ooof">This is also link</a>
<iframe src="https://example.com/ooof"></iframe>
<iframe src="https://example.com/yikes/138fskg"></iframe>
<iframe src="https://example.com/biggie"></iframe>


</body>
</html>

这是我能做到的

string videoLink = wc.DownloadString(link);
bool contain = videoLink.Contains("https://example.com/yikes/");
if (contain == true)
{ 
      //Copy whole link
}

谢谢你的帮助!

标签: c#webclient

解决方案


据我了解,您希望拥有以 . 开头的链接"https://example.com/yikes/"

string[] videoLinks = new string[5]
{
"<iframe src='https://example.com/ooof'></iframe>",
"<iframe src='https://example.com/yikes/138fskg'></iframe>",
"<iframe src='https://example.com/biggie'></iframe>",
"<iframe src='https://example.com/yikes/138fskg/1231'></iframe>",
"<iframe src='https://example.com/yikes/138fskg/12314531'></iframe>",
};
string link = "https://example.com/yikes/";

var matchedValues = videoLinks.Where(v => v.Contains(link));
foreach (var item in matchedValues)
{
    Console.WriteLine(item);
}

输出:

<iframe src='https://example.com/yikes/138fskg'></iframe>
<iframe src='https://example.com/yikes/138fskg/1231'></iframe>
<iframe src='https://example.com/yikes/138fskg/12314531'></iframe>

推荐阅读