【发布时间】:2018-01-09 22:45:02
【问题描述】:
我在具有类似 html 的网站上进行抓取
<a href="/pages/1></a>
我也有 window.location 对象,我有
origin:"http://www.example.org"
所以我可以构建像origin + href = http://www.example.org/pages/1 这样的绝对路径
我在我的文件系统上制作了一个页面模型以供测试。
-www.example.org
|-2017
|-pages
|-1.html
|-2.html
|-2016
|-pages
|-1.html
|-2.html
在这些 html 文件中,链接看起来像这样:
<!-- www.example.org/2016/pages/1.html -->
<a href="../../2017/pages/1.html">2017</a>
在测试中,同样的代码不会起作用,因为 window.location 对象的来源是file://:
hash:""
host:""
hostname:""
href:"file:///home/me/projects/fp/src/test/fixtures/www.example.org/2016/pages/1.html"
origin:"file://"
pathname:"/home/me/projects/fp/src/test/fixtures/www.example.org/2016/pages/1.html"
port:""
protocol:"file:"
产生 origin + href = file://../../2017/pages/1.html 。如果协议是 file: ,通过一些字符串操作,我可以从 location.pathname 制作 file:///home/me/projects/fp/src/test/fixtures/www.example.org/2017/pages/1.html 。但这是处理这个问题的正确方法吗?
【问题讨论】:
标签: javascript html path