---
title: "网络爬虫-如何将相对路径转为绝对路径"
author: "johnson.lee"
date: "2010-01-09T09:26:05Z"
tags: ["ASP", "ASP.net", "OS", "Web", "浏览器"]
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2010-01-09T09:26:05Z"
id: 566320
url: "https://johnson-lee.iteye.com/blog/566320"
---

最近在写一个蜘蛛程序，碰到了一个问题，就是如何将页面内超链接的相对路径转为绝对路径，Google了一遍，有很多例子，看了一下，全是不太正确的做法，考虑太片面了，它们的做法大致是这样： 如当前页面是：http://localhost:5995/web/index.html 代码如下： \[code="java"] String url = "http://localhost:5995/web/index.html"; int offset = url.lastIndexOf("/"); String cate = url.substring(0, 0ffset + 1); String absPath = cate + "页面内的相对路径"; \[/code] 上面的代码只是在当前请求的路径不是服务器端的目录时适用，有些站点没有屏蔽对目录的访问，就拿上面的url做例子，当前站点的目录结构如下： web ├─images │ ├─logo.gif │ ├─bg.gif │ ├─banner.swf │ └─style.css ├─scripts │ ├─swfobject.js │ ├─common.js │ └─AjaxRequest.js └─upload ├─201001090001.rar ├─201001090002.rar ├─201001090003.rar ├─201001090004.rar └─201001090005.rar 假设此站点没有屏蔽对目录的访问，那么在浏览器上请求http://localhost:5995/web/scripts,查看源代码： \[code="html"] 目录清单 -- /web/scripts/ -------------------------------------------------------------------------------- \[To Parent Directory] Wednesday, December 16, 2009 12:48 PM 8,670 swfobject.js Monday, December 07, 2009 05:09 PM 1,450 common.js Thursday, December 31, 2009 01:54 PM 3,309 AjaxRequest.js -------------------------------------------------------------------------------- 版本信息: ASP.NET Development Server 8.0.0.0 \[/code] 查看源代码： \[code="html"] 目录清单 -- /web/scripts/

## *目录清单 -- /web/scripts/*

* * *

```
[To Parent Directory]

 Wednesday, December 16, 2009 12:48 PM        8,670 swfobject.js
    Monday, December 07, 2009 05:09 PM        1,450 common.js
  Thursday, December 31, 2009 01:54 PM        3,309 AjaxRequest.js
```

* * *

**版本信息:** ASP.NET Development Server 8.0.0.0 \[/code] 蜘蛛程序分析到"swfobject.js","common.js","AjaxRequest.js"后，如果按上面截取的做法，肯定就错了。这里就不能截取最后的"/"，应该是"http://localhost:5995/web/scripts" + "/" + "swfobject.js"，但是浏览器为什么能准确分析出来呢？当在页面http://localhost:5995/web/scripts上将鼠标放在超链接\[u]swfobject.js\[/u]上，状态栏显示了它的绝对路径为http://localhost:5995/web/scripts/swfobject.js而不是http://localhost:5995/web/swfobject.js。于是我猜想，肯定在HTTP响应头信息里有相应的信息，于是用下面的代码测试： \[code="java"] try { URL base = new URL("http://localhost:5995/web/scripts"); Map&gt; props = base.openConnection().getHeaderFields(); for (Iterator iterator = props.keySet().iterator(); iterator.hasNext();) { String key = (String) iterator.next(); System.out.println(key + "=" + props.get(key)); } } catch (IOException e) { e.printStackTrace(); } \[/code] 打印信息如下： \[code="java"] localhost:5995 null=\[HTTP/1.1 200 OK] Date=\[Sat, 09 Jan 2010 01:00:25 GMT] Content-type=\[text/html; charset=utf-8] Content-Length=\[1494] Connection=\[Close] Server=\[ASP.NET Development Server/8.0.0.0] \[/code] 从打印内容里，根本看不出里面有什么东西可以区别目录和html文件的，后来又想，如果用Socket模拟浏览器请求，或许有意想不到的收获呢！先贴代码： \[code="java"] public static void main(String\[] args) { try { Socket s = new Socket("127.0.0.1", 5995); OutputStream os = s.getOutputStream(); os.write(("GET /web/scripts HTTP/1.1\\r\\n" + "Host: localhost\\r\\n" + "Connection: keep-alive\\r\\n" + "\\r\\n").getBytes()); os.flush(); InputStream is = s.getInputStream(); int c = -1; while ((c = is.read()) != -1) { System.out.print((char)c); } os.close(); is.close(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } \[/code] 打印信息如下： \[code="html"] HTTP/1.1 302 Found Server: ASP.NET Development Server/8.0.0.0 Date: Sat, 09 Jan 2010 01:11:42 GMT Content-Length: 135 Location: /web/scripts/ Connection: Close Object moved

## Object moved to [here](/website1/scripts/).

\[/code] 我想不用我说大家都知道了吧？关键在于浏览器第一次请求http://localhost:5995/web/scripts时，服务器给浏览器一个302的响应，告诉浏览器这个资源被移动到别的地方了，你访问这个路径吧：Location: /web/scripts/，于是浏览器再请求http://localhost:5995/web/scripts/这次后面就多了个"/"，于是浏览器就知道这个页面内的所有相对路径的超链接的绝对路径了。^\_^ 现在知道了用URLConnection测试时，它做了二次请求，所以看不到302的响应。