当我从某些页面抓取内容时,脚本会给出一个相对URL。是否可能使用Simple HTML DOM获取绝对URL?
当我从某些页面抓取内容时,脚本会给出一个相对URL。是否可能使用Simple HTML DOM获取绝对URL?
BASE
元素)。然后获取每个URI引用并应用在RFC 3986中描述的解析相对URI的算法(已经有可用于此的类,例如PEAR包Net_URL2)。$uri = new Net_URL2('http://example.com/foo/bar'); // URI of the resource
$baseURI = $uri;
foreach ($html->find('base[href]') as $elem) {
$baseURI = $uri->resolve($elem->href);
}
foreach ($html->find('*[src]') as $elem) {
$elem->src = $baseURI->resolve($elem->src)->__toString();
}
foreach ($html->find('*[href]') as $elem) {
if (strtoupper($elem->tag) === 'BASE') continue;
$elem->href = $baseURI->resolve($elem->href)->__toString();
}
foreach ($html->find('form[action]') as $elem) {
$elem->action = $baseURI->resolve($elem->action)->__toString();
}
对于包含类似background
、cite
、classid
、codebase
、data
、longdesc
、profile
和usemap
等URI的任何其他属性,都需要重复进行替换(请参见HTML 4.01中属性的索引)。
<base href="http://example.com">
,这将把指定的href
作为文档中所有相对URL的基本URL。请参阅http://www.w3schools.com/tags/tag_base.asp。