我喜欢了解如何使用<base href="" />
对我的网络爬虫来说很有价值,所以我用主要浏览器测试了几种组合,最后发现了一些我不明白的带有双斜杠的东西。
如果您不喜欢阅读所有内容,请跳至测试结果D and E。所有测试演示:
http://gutt.it/basehref.php http://gutt.it/basehref.php
一步一步我的通话测试结果http://example.com/images.html
:
A - 多个基本 href
<html>
<head>
<base target="_blank" />
<base href="http://example.com/images/" />
<base href="http://example.com/" />
</head>
<body>
<img src="/images/image.jpg">
<img src="image.jpg">
<img src="./image.jpg">
<img src="images/image.jpg"> not found
<img src="/image.jpg"> not found
<img src="../image.jpg"> not found
</body>
</html>
结论
- 只有第一个
<base>
with href
counts
- 来源以
/
目标为根
-
../
向上移动一个文件夹
B - 没有尾部斜线
<html>
<head>
<base href="http://example.com/images" />
</head>
<body>
<img src="/images/image.jpg">
<img src="image.jpg"> not found
<img src="./image.jpg"> not found
<img src="images/image.jpg">
<img src="/image.jpg"> not found
<img src="../image.jpg"> not found
</body>
</html>
结论
-
<base href>
忽略最后一个斜杠之后的所有内容,所以http://example.com/images
变成http://example.com/
C——应该怎样
<html>
<head>
<base href="http://example.com/" />
</head>
<body>
<img src="/images/image.jpg">
<img src="image.jpg"> not found
<img src="./image.jpg"> not found
<img src="images/image.jpg">
<img src="/image.jpg"> not found
<img src="../image.jpg"> not found
</body>
</html>
结论
D - 双斜线
<html>
<head>
<base href="http://example.com/images//" />
</head>
<body>
<img src="/images/image.jpg">
<img src="image.jpg">
<img src="./image.jpg">
<img src="images/image.jpg"> not found
<img src="/image.jpg"> not found
<img src="../image.jpg">
</body>
</html>
E - 带空格的双斜杠
<html>
<head>
<base href="http://example.com/images/ /" />
</head>
<body>
<img src="/images/image.jpg">
<img src="image.jpg"> not found
<img src="./image.jpg"> not found
<img src="images/image.jpg"> not found
<img src="/image.jpg"> not found
<img src="../image.jpg">
</body>
</html>
两者都不是“有效”URL,而是我的网络爬虫的真实结果。请解释一下发生了什么D and E that ../image.jpg
可以找到,为什么会导致空白差异?
只为您的兴趣:
-
<base href="http://example.com//" />
是相同的Test C
-
<base href="http://example.com/ /" />
是完全不同的。仅有的../image.jpg
被发现
-
<base href="a/" />
只找到/images/image.jpg