相对于HTML中的绝对路径 [英] Relative to absolute paths in HTML
本文介绍了相对于HTML中的绝对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要通过URL创建新闻通讯.为此,我:
I need to create a newsletters by URL. To do that, I:
- 创建一个
WebClient
. - 使用WebClient的方法
DownloadData
获取字节数组中的页面源; - 从source-html字节数组中获取字符串,并将其设置为新闻通讯内容.
- Create a
WebClient
. - Use WebClient's method
DownloadData
to get a source of page in byte array; - Get string from the source-html byte array and set it to the newsletter content.
但是,我在使用路径时遇到了一些麻烦.所有元素的来源都是相对的(/img/welcome.png
),但我需要一个绝对的来源,例如 http://www.example.com/img/welcome.png .
However, I have some troubles with paths. All elements' sources were relative (/img/welcome.png
) but I need an absolute one, like http://www.example.com/img/welcome.png.
我该怎么做?
推荐答案
One of the possible ways to resolve this task is the use the HtmlAgilityPack library.
一些示例(修复链接):
WebClient client = new WebClient();
byte[] requestHTML = client.DownloadData(sourceUrl);
string sourceHTML = new UTF8Encoding().GetString(requestHTML);
HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(sourceHTML);
foreach (HtmlNode link in htmlDoc.DocumentNode.SelectNodes("//a[@href]"))
{
if (!string.IsNullOrEmpty(link.Attributes["href"].Value))
{
HtmlAttribute att = link.Attributes["href"];
att.Value = this.AbsoluteUrlByRelative(att.Value);
}
}
这篇关于相对于HTML中的绝对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文