首先:抱歉我的英语不好!
我知道标题不是最好的英文,但我真的不知道如何格式化这个问题...
我想要做的是逐行读取HTML源代码,这样当它看到一个给定的单词时(比如http://)它复制整个句子,所以我可以剥去其余的只保留URL.
这就是我尝试过的:
using (var source = new StreamReader(TempFile))
{
string line;
while ((line = source.ReadLine()) != null)
{
if (line.Contains("http://"))
{
Console.WriteLine(line);
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我想从外部文件中读取它,但是当我想读取字符串或字符串构建器时它不起作用,那么这种方法是完美的,你如何逐行阅读?
您可以使用a new StringReader(theString)来做到这一点string,但我质疑您的整体策略.使用像HTML Agility Pack这样的工具可以做得更好.
例如,这里是HTML Agility Pack提取所有超链接:
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(theString);
foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[@href]")
{
HtmlAttribute att = link["href"];
Console.WriteLine(att.Value);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8093 次 |
| 最近记录: |