2016-07-28 52 views
0

我想从页面源文件中提取一些html数据。这是参考。链接有一个HTML链接查看源:http://www.4icu.org/reviews/index2.htm。我想问一下,我怎样才能用JAVA提取大学的名称和国家名称。我知道如何提取大学名称的方法,但是如何通过在class =“i”时扫描表格来提高程序的速度,并提取国家(即美国)与< ...... “alt =”United States“/>JAVA解析表格数据

<tr> 
<td><a name="UNIVERSITIES-BY-NAME"></a><h2>A-Z list of world Universities and Colleges</h2> 
</tr> 

<tr> 
<td class="i"><a href="/reviews/9107.htm"> A.T. Still University</a></td> 
<td width="50" align="right" nowrap>us <img src="/i/bg.gif" class="fl flag-us" alt="United States" /></td> 
</tr> 

在此先感谢。

编辑 按照什么@ 11thdimension说,这里是我的java文件

public class University { 
    public static void main(String[] args) throws Exception { 
     System.out.println("Started"); 

     URL url = new URL ("http://www.4icu.org/reviews/index2.htm"); 

     URLConnection spoof = url.openConnection();   
     // Spoof the connection so we look like a web browser 
     spoof.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; H010818)"); 

     String connect = url.toString(); 
     Document doc = Jsoup.connect(connect).get(); 

     Elements cells = doc.select("td.i"); 

     Iterator<Element> iterator = cells.iterator(); 

     while (iterator.hasNext()) { 
      Element cell = iterator.next(); 
      String university = cell.select("a").text(); 
      String country = cell.nextElementSibling().select("img").attr("alt"); 

      System.out.printf("country : %s, university : %s %n", country, university); 
     } 
    } 
} 

然而,当我运行它,它给了我下面的错误。

Started 
Exception in thread "main" org.jsoup.HttpStatusException: HTTP error fetching URL. Status=403, URL=http://www.4icu.org/reviews/index2.htm 

EDIT2 我已经创建了下面的程序,以获取HTML网站的标题。

public class Get_Header { 
    public static void main(String[] args) throws Exception { 
    URL url = new URL("http://www.4icu.org/reviews/index2.htm"); 
    URLConnection connection = url.openConnection(); 

    Map responseMap = connection.getHeaderFields(); 
    for (Iterator iterator = responseMap.keySet().iterator(); iterator.hasNext();) { 
     String key = (String) iterator.next(); 
     System.out.println(key + " = "); 

     List values = (List) responseMap.get(key); 
     for (int i = 0; i < values.size(); i++) { 
     Object o = values.get(i); 
     System.out.println(o + ", "); 
     } 
    } 
    } 
} 

它重新调整以下结果。

X-Frame-Options = 
SAMEORIGIN, 
Transfer-Encoding = 
chunked, 
null = 
HTTP/1.1 403 Forbidden, 
CF-RAY = 
2ca61c7a769b1980-HKG, 
Server = 
cloudflare-nginx, 
Cache-Control = 
max-age=10, 
Connection = 
keep-alive, 
Set-Cookie = 
__cfduid=d4f8d740e0ae0dd551be15e031359844d1469853403; expires=Sun, 30-Jul-17 04:36:43 GMT; path=/; domain=.4icu.org; HttpOnly, 
Expires = 
Sat, 30 Jul 2016 04:36:53 GMT, 
Date = 
Sat, 30 Jul 2016 04:36:43 GMT, 
Content-Type = 
text/html; charset=UTF-8, 

虽然我可以得到头,但我应该如何在编辑和EDIT2代码结合起来,形成一个完整的?谢谢。

+0

你需要做一次或那将是一个重复性测试k? – 11thdimension

+0

解决方案需要多长时间才能证明暂停问题的正确性? – 11thdimension

+0

我编辑了这个问题,以缩小我的问题。谢谢 –

回答

1

如果这将是一个单一的时间任务,那么你应该使用JavaScript fot它。

以下代码将在控制台中记录所需的名称。您必须在浏览器控制台中运行它。

(function() { 
    var a = []; 
    document.querySelectorAll("td.i a").forEach(function (anchor) { a.push(anchor.textContent.trim());}); 

    console.log(a.join("\n")); 
})(); 

以下为Jsoup selectors

Maven的依赖

<dependencies> 
    <dependency> 
     <groupId>org.jsoup</groupId> 
     <artifactId>jsoup</artifactId> 
     <version>1.8.3</version> 
    </dependency> 
</dependencies> 

的Java代码的Java例子

import java.io.File; 
import java.util.Iterator; 

import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.nodes.Element; 
import org.jsoup.select.Elements; 

public class TestJsoup { 
    public static void main(String[] args) throws Exception { 
     System.out.println("Starteed"); 

     File file = new File("A-Z list of 11930 World Colleges & Universities.html"); 
     Document doc = Jsoup.parse(file, "UTF-8"); 

     Elements cells = doc.select("td.i"); 

     Iterator<Element> iterator = cells.iterator(); 

     while (iterator.hasNext()) { 
      Element cell = iterator.next(); 
      String university = cell.select("a").text(); 
      String country = cell.nextElementSibling().select("img").attr("alt"); 

      System.out.printf("country : %s, university : %s %n", country, university); 
     } 
    } 
} 
+0

谢谢。该程序将运行多次,因为在http链接中需要更改各种索引号。只是好奇我怎么能用java来抓住“alt = united states”中的国家级数据。谢谢 –

+0

添加代码来提取国家。 – 11thdimension

+0

感谢您的帮助。但是,当我将链接http://www.4icu.org/reviews/index2.htm插入到11930 World Colleges&Universities.html的AZ列表替换位置时,​​它给我在线程“main”java中的异常。 io.FileNotFoundException:www.4icu.org \ reviews \ index2.htm 我修改了我的问题,使其更清晰。 –