【问题标题】:parsing an url for crawler解析爬虫的 url
【发布时间】:2010-09-06 15:18:06
【问题描述】:

我正在编写一个小型爬虫,在获取链接的同时提取大约 5 到 10 个网站,我得到了一些这样的网址

../tets/index.html

如果是/test/index.html,我们可以添加基本网址http://www.example.com/test/index.html

我可以为这种网址做什么。

【问题讨论】:

  • 您尝试使用哪种语言?

标签: php c++ url urlparse


【解决方案1】:

这样的网址是 relative urls 。 “..”表示“父目录”,而“.”仅表示“此目录”,如 bash。 例如,如果您正在查看此页面:http://www.someserver/test/foo/bar.html,并且其中有一个这样的 url:“../baz/foobar.html”,我认为它实际上会指向http://www.someserver/test/baz/foobar.html。只是测试。

【讨论】:

    【解决方案2】:

    使用dirname() 获取基本目录,使用substr() 删除.. 并将其附加到那里。像这样:

    <?php
    $url = "../tets/index.html";
    $currentURL = "http://example.com/somedir/anotherdir";
    echo dirname($currentURL).substr($url, 2);
    ?>
    

    这个输出:

    http://example.com/somedir/tets/index.html

    【讨论】:

    • @greg0ire,这个问题也被标记为PHP。请看一下标签。
    • 哎呀,真的!这很奇怪......赞成 bjskishore123 的评论
    【解决方案3】:

    看看这个URL Normalization维基百科页面。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-12
      • 2012-07-14
      • 2019-04-17
      • 1970-01-01
      • 1970-01-01
      • 2013-09-23
      • 2013-05-04
      • 1970-01-01
      相关资源
      最近更新 更多