2014-04-30 14 views
0

我正在使用Jsoup來解析網站。我在解析類:Jsoup沒有檢測到引號中的文本

<td class="tl"> 
<script> document.write(Icons.GetShortDescription(1, 'CurrentWeather'));</script> 
"Despejado"<span class="details"> 
</span> 
</td> 

Jsoup無法檢測到文本「Despejado」。下面是相關的代碼:

url="http://freemeteo.ar.com/eltiempo/mendoza/historia/historial-diario/?gid=3844421&date=2010-07-02&station=23812&language=spanishar&country=argentina"; 
    doc = Jsoup.connect(url).get(); 
    i=0; 
    Elements lineks = doc.select("table.daily-history"); 
    for (Element linek : lineks) { 
     Elements datos=linek.select("tbody"); 
        for(Element dato : datos){ 
         Elements datos5 = dato.select("td.tl"); 
              System.out.println("code class:" + datos5.html()); 

            } 
    } 

輸出是: 「despejado」

code class: <script> 
      document.write(Icons.GetShortDescription(1, 'CurrentWeather')); 
     </script><span class="details"> </span> 

Jsoup不讀問題是什麼?

  • bug Jsoup?
  • 問題是網站?

請幫助我瞭解如何讀課文 「despejado」?**

回答

0

好吧我知道了。

Jsoup無法獲得「despejado」,因爲它在網站上不存在,直到JavaScript腳本放入。所以Jsoup沒有選擇或獲取。 Jsoup是一個不是JavaScript解析的html解析器。但是,我想我已經明白了。

JavaScript腳本的在頂部聲明,而且如果你去看看你會看到放「despejado」和頁面上的其他說明腳本:

<script type="text/javascript" src="/Services/IconDescriptions/Index/37/g.js"></script> 

好了,所以如果你去看看在該腳本你會看到這個巨大的腳本文件,下面是它的一些:

var Icons = { 
    "Forecast":{ 
     "1":{"Description":"Buen tiempo","ShortDescription":"Despejado"}, 
     "2":{"Description":"Pocas nubes","ShortDescription":"Pocas nubes"}, 
     "3":{"Description":"Cielos parcialmente cubiertos","ShortDescription":"Parcialmente cubierto"}, 
     "4":{"Description":"Cielos cubiertos","ShortDescription":"Cubierto"}, 

...和喜歡150多個

好了,現在知道這一點,你可以使用這個:

Elements elements = doc.select("table.daily-history tbody td.tl script"); 

     int number; 
     String numberString; 

     for (Element element: elements){ 

      // here's what you had 
      System.out.println("code class: " + element.html()); 

      // get the html as a string 
      numberString = element.html(); 

      // isolate the number you need 
      numberString = numberString.substring(numberString.lastIndexOf("(")+1,numberString.lastIndexOf(" ") -1); 

      // parse to integer 
      number = Integer.valueOf(numberString); 
      System.out.println("number: " + number); 


     } 

我保留了額外的String代碼,以幫助您理解。所以,這裏的系統輸出:

code class: document.write(Icons.GetShortDescription(1, 'CurrentWeather')); 
number: 1 

現在,您可以使用「數字」,這是「1」,它的交叉參考JavaScript文件,以獲得「簡要說明」,這是「despejado」。我檢查了日曆上的其他幾個日期以瞭解不同的情況,並且它可以工作。

我希望有一個更簡單的方法,但這將工作。如果您也許可以找到該網站的純文本版本,那應該很容易。網站有時會爲他們的屏幕閱讀器的盲人提供更簡單的版本。祝你好運!

+0

很好!!!!感謝您的幫助,非常巧妙的解決方案。 – user3588102

+0

Pd:你是天才;) – user3588102

+0

沒問題。如果確實有效,那麼可以隨時接受答案。 – Everett