c# - 在源代码中查找链接并复制整个链接 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
}
谢谢你的帮助!
解决方案
据我了解,您希望拥有以 . 开头的链接"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>
推荐阅读
- angular - 在 Angular 中实时更新数据
- vba - 是否有包含临时轴的 View.GetOutline() 的替代方法?
- c# - 使用 foo[bar] = baz 添加到 ConcurrentDictionary 是否线程安全?
- java - Eclipse 无法在 Raspberry Pi 4 上运行:java.lang.ClassNotFoundException
- swift - 将 Codable 转换为 Json(字典)Swift
- java - 有没有办法增加 Maven pom.xml 的属性值?
- node.js - es6节点未正确导入
- drupal - URI 'http://' 在 Drupal\Core\Url::fromUri() 中格式错误
- java - JNIEnv FindClass 找不到我的包类
- ssis - 如何仅提取 TIME 的 DATETIME 字段并将其转换为 ssis (ssdt) 中的 INT