[英]JAVA parsing table data
我想從頁面源中提取一些html數據。 這是裁判。 鏈接具有html鏈接視圖源: http : //www.4icu.org/reviews/index2.htm 。 我想問一下如何使用JAVA僅提取大學名稱和國家/地區名稱。 我知道只提取大學名稱之間的方法,但是如何通過在class =“ i”時掃描表並使用<...提取國家/地區,也就是美國,來使程序更快? alt =“美國” />
<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,
雖然我可以獲取標題,但是如何將EDIT和EDIT2中的代碼結合起來形成一個完整的標題呢? 謝謝。
如果這將是一次任務,那么您可能應該使用Javascript。
以下代碼將在控制台中記錄所需的名稱。 您必須在瀏覽器控制台中運行它。
(function () {
var a = [];
document.querySelectorAll("td.i a").forEach(function (anchor) { a.push(anchor.textContent.trim());});
console.log(a.join("\n"));
})();
以下是帶有Jsoup選擇器的Java示例
Maven依賴
<dependencies>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.8.3</version>
</dependency>
</dependencies>
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);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.