简体   繁体   English

File://协议的绝对相对路径

[英]relative path to absolute with file:// protocol

I do scraping on a site which has similar html 我在具有类似html的网站上抓取

<a href="/pages/1></a>

I also have the window.location object, where I have 我也有window.location对象

origin:"http://www.example.org"

so I can build the absolute path like origin + href = http://www.example.org/pages/1 所以我可以建立绝对路径,例如origin + href = http://www.example.org/pages/1

I made a mockup of the page on my file system for testing. 我在文件系统上制作了页面的模型以进行测试。

-www.example.org
  |-2017
    |-pages
      |-1.html
      |-2.html
  |-2016
    |-pages
      |-1.html
      |-2.html

in those html files the links look something like this: 在这些html文件中,链接看起来像这样:

<!-- www.example.org/2016/pages/1.html --> <a href="../../2017/pages/1.html">2017</a>

In the test the same code won't work, because the window.location object's origin is file:// : 在测试中,相同的代码将不起作用,因为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:"

which produces origin + href = file://../../2017/pages/1.html . 产生origin + href = file://../../2017/pages/1.html。 With some string manipulation I could make file:///home/me/projects/fp/src/test/fixtures/www.example.org/2017/pages/1.html from location.pathname if the protocol is file: . 如果协议是file: file:///home/me/projects/fp/src/test/fixtures/www.example.org/2017/pages/1.html则可以通过一些字符串操作从location.pathname创建file:///home/me/projects/fp/src/test/fixtures/www.example.org/2017/pages/1.html But is it the right way to handle this problem? 但这是解决此问题的正确方法吗?

file:// can be used only for absolute paths. file://仅可用于绝对路径。

Only relative path that works is the current working directory. 当前工作目录仅适用于相对路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM