【问题标题】:How to get fully-qualified URL from anchor href?如何从锚点 href 获取完全限定的 URL?
【发布时间】:2015-04-03 13:31:59
【问题描述】:

我正在用 php 编写一个网络爬虫。给定一个当前 URL,以及一组指向绝对 URL、相对 URL 和根 URL 的链接,我将如何确定每个链接的完全限定 URL?

例如,假设我正在抓取 URL:

http://www.example.com/path/to/my/file.html

网页包含的链接数组是:

array(
    'http://www.some-other-domain.com/',
    '../../',
    '/search',
);

如何确定每个链接的完全限定 URL?我在这个例子中寻找的结果分别是:

http://www.some-other-domain.com/
http://www.example.com/path/
http://www.example.com/search/

【问题讨论】:

  • 不要忘记您可能还需要考虑其他一些因素——比如 f.e. HTML 文档中的<base href="…">。这并不像您想象的那么简单(从您的问题看来,您一开始并没有想那么难……)

标签: php html url anchor href


【解决方案1】:

我认为最简单的方法是使用这样的库: http://www.electrictoolbox.com/php-resolve-relative-urls-absolute/

链接中的示例:

url_to_absolute('http://www.example.com/sitemap.html', 'aboutus.html');

解析为http://www.example.com/aboutus.html

url_to_absolute('http://www.example.com/content/sitemap.html', '../images/somephoto.jpg');

解析为http://www.example.com/images/somephoto.jpg

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-23
    • 2012-11-07
    • 2012-03-18
    • 1970-01-01
    • 1970-01-01
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多