【发布时间】:2011-08-26 04:45:11
【问题描述】:
我正在尝试使用 java 获取基本 URL。我在代码中使用了 jtidy 解析器来获取标题。我使用 jtidy 正确获取标题,但没有从给定 URL 获取基本 URL。
我有一些 URL 作为输入:
String s1 = "http://staff.unak.is/andy/GameProgramming0910/new_page_2.htm";
String s2 = "http://www.complex.com/pop-culture/2011/04/10-hottest-women-in-fast-and-furious-movies";
我想从第一个字符串中获取 "http://staff.unak.is/andy/GameProgramming0910/" 作为基本 URL,从第二个字符串中获取 "http://www.complex.com/" 作为基本 URL。
我正在使用代码:
URL url = new URL(s1);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream in = conn.getInputStream();
Document doc = new Tidy().parseDOM(in, null);
String titleText = doc.getElementsByTagName("title").item(0).getFirstChild()
.getNodeValue();
我收到了titletext,但请告诉我如何从上面给定的 URL 获取基本 URL?
【问题讨论】:
-
哪些规则会告诉您
http://www.complex.com/是基本网址而不是http://www.complex.com/pop-culture/2011/04/?