2014-02-06 62 views
-1

我已經經歷了多篇關於解析等的文章。我看到的大多數回覆都是建議人們使用圖書館或其他東西。我現在的問題是創建一個算法,將獲取我想要的確切信息。我的目的是從天氣網站獲取2個關閉學校的狀態。我開始使用Jsoup作爲推薦人,但我需要幫助。Java使用Jsoup從網站讀取信息

網頁:Click here

圖片:Click here

網頁源的例子:click here

我也許可以弄清楚如何獲取文本的特定行的網頁上,因爲我已經知道名字我正在尋找的學校,但2線下來是我所需要的地位。如果每個學校都有一定的地位,但都是封閉式的或​​兩小時延遲的,那麼這很容易,所以我不能僅僅爲此尋找答案。我想要一些想法或答案,我可以如何apporach這一點。我將這樣做2次,因爲我想要查找2所學校。我已經有了我可以用來查看他們的名字,我只需要這個地位。

這是我想要做的一個例子。 (須藤代碼)

Document doc = connect(to url); 
Element schoolName1 = doc.lookForText(htmlLineHere/schoolname); 

String status1 = schoolName.getNext().text();//suppose this gets the line right after which should be my status and then cleans off the Html. 

這就是我現在所擁有的

public static SchoolClosing lookupDebug() throws IOException { 
     final ArrayList<String> Status = new ArrayList<String>(); 

     try { 
      //connects to my wanted website 
      Document doc = Jsoup.connect("http://www.10tv.com/content/sections/weather/closings.html").get(); 
      //selects/fetches the line of code I want 
      Element schoolName = doc.html("<td valign="+"top"+">Athens City Schools</td>"); 
      //an array of Strings where I am going to add the text I need when I get it 
      final ArrayList<String> temp = new ArrayList<String>(); 
      //checking if its fetching the text 
      System.out.println(schoolName.text()); 
      //add the text to the array 
      temp.add(schoolName.text()); 
      for (int i = 0; i <= 1; i++) { 
       final String[] tempStatus = temp.get(i).split(" "); 
       Status.add(tempStatus[0]); 
      } 
     } catch (final IOException e) { 
      throw new IOException("There was a problem loading School Closing Status"); 
     } 
     return new SchoolClosing(Status); 
    } 
+0

任何協助將有助於請! – Yuriah

+0

所有在這裏提出問題的人都希望對他們的問題提供某種幫助。無需大聲呼喊。 – eis

+0

我很抱歉,它沒有得到任何關注,我昨天發佈了這個。 – Yuriah

回答

2
Document doc = Jsoup.connect(
     "http://www.10tv.com/content/sections/weather/closings.html") 
     .get(); 
for (Element tr : doc.select("#closings tr")) { 
    Element tds = tr.select("td").first(); 
    if (tds != null) { 
     String county = tr.select("td:eq(0)").text(); 
     String schoolName = tr.select("td:eq(1)").text(); 
     String status = tr.select("td:eq(2)").text(); 
     System.out.println(String.format(
       "county: %s, schoolName: %s, status: %s", county, 
       schoolName, status)); 
    } 
} 

輸出:

county: Athens, schoolName: Beacon School, status: Two-hour Delay 
county: Franklin, schoolName: City of Grandview Heights, status: Snow Emergency through 8pm Thursday 
county: Franklin, schoolName: Electrical Trades Center, status: All Evening Activities Cancelled 
county: Franklin, schoolName: Hilock Fellowship Church, status: PM Services Cancelled 
county: Franklin, schoolName: International Christian Center, status: All Evening Activities Cancelled 
county: Franklin, schoolName: Maranatha Baptist Church, status: PM Services Cancelled 
county: Franklin, schoolName: Masters Commission New Covenant Church, status: Bible Study Cancelled 
county: Franklin, schoolName: New Life Christian Fellowship, status: All Activities Cancelled 
county: Franklin, schoolName: The Epilepsy Foundation of Central Ohio, status: All Evening Activities Cancelled 
county: Franklin, schoolName: Washington Ave United Methodist Church, status: All Evening Activities Cancelled 

或循環:

for (Element tr : doc.select("#closings tr")) { 
    System.out.println("----------------------"); 
    for (Element td : tr.select("td")) { 
     System.out.println(td.text()); 
    } 
} 

給出:

---------------------- 
Athens 
Beacon School 
Two-hour Delay 
---------------------- 
Franklin 
City of Grandview Heights 
Snow Emergency through 8pm Thursday 
---------------------- 
Franklin 
Electrical Trades Center 
All Evening Activities Cancelled 
... 
+0

謝謝你的光輝燦爛的答案!希望我能表達我多麼感恩。我可以做你需要的,因爲你完美地格式化了它。 – Yuriah

+0

這就是我所製作的,它是我的程序的一部分。 http://i.imgur.com/geEalQK.png – Yuriah

+0

祝你好運!高興我可以幫助:) –