2013-02-25 67 views
1

儘管我嘗試使用Nokogiri,Mechanize和XPath解析頁,但是我收到一個空數組。使用Nokogiri返回空數組的XPath

Page I'm trying to Parse.

我檢查它在Chrome並獲得了XPath,然後嘗試多種方法來分析它,但總是收到一個空數組。

我想:

puts page.search('/html/body/div/table/tbody/tr[2]/td/table/tbody/tr[2]/td[2]').inspect 

puts post_page.parser.xpath('/html/body/div/table/tbody/tr[2]/td/table/tbody/tr[2]/td[2]').inspect 

puts post_page.parser.at_xpath('/html/body/div/table/tbody/tr[2]/td/table/tbody/tr[2]/td[2]').inspect 

所有有和沒有尾隨 「/文本」

這是我試圖刮掉頁面的源:

<SCRIPT language="JavaScript"> 
<!-- 
document.cookie = "IV_JCT=%2FMPIS; path=/"; 
//--> 
</SCRIPT> 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 

    <head> 
    <title>My Schedule</title> 

    <meta http-equiv="pragma" content="no-cache"> 
    <meta http-equiv="cache-control" content="no-cache"> 
    <meta http-equiv="expires" content="-1">  
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
    <meta http-equiv="description" content="This is my schedule"> 
    <!-- 
    <link rel="stylesheet" type="text/css" href="styles.css"> 
    --> 

    </head> 

    <body> 
    <div align="center"> 
    <strong>My Schedule</strong><br>as of Sun Feb 24 2013 06:43:09 PM CST<br><br> 
    <div align="left"><pre><br>Employee Name: Johnson Appleseed  
Unit = 12345</pre>  
    <br> 
    </div> 

    <table border="0" cellpadding="0" cellspacing="0" width="100%"> 
    <tr> 
    <td colspan="8" align="center"><b><font size="+1">Schedules may be subject to change based on business needs or demand</font></b></td> 
    </tr> 

     <tr><td> 
     <table border="4" bordercolor="#2D73B9" cellpadding="2" cellspacing="2" width="100%"> 
     <tr bgcolor="#7C9BCF"> 
     <td width="12%" align="center"><b>Sunday</b></td> 
     <td width="12%" align="center"><b>Monday</b></td> 
     <td width="12%" align="center"><b>Tuesday</b></td> 
     <td width="12%" align="center"><b>Wednesday</b></td> 
     <td width="12%" align="center"><b>Thursday</b></td> 
     <td width="12%" align="center"><b>Friday</b></td> 
     <td width="12%" align="center"><b>Saturday</b></td> 
     <td rowspan="2" width="12%" align="center"><b>Total weekly Hours</b></td> 
     </tr> 

     <tr bgcolor="#7C9BCF"> 

     <td width="14%" align="center">2013-02-24</td> 

     <td width="14%" align="center">2013-02-25</td> 

     <td width="14%" align="center">2013-02-26</td> 

     <td width="14%" align="center">2013-02-27</td> 

     <td width="14%" align="center">2013-02-28</td> 

     <td width="14%" align="center">2013-03-01</td> 

     <td width="14%" align="center">2013-03-02</td> 

     </tr> 

     <tr bgcolor="#FFFFFF"> 

     <td width="14%" align="left"><pre>&nbsp;</pre></td> 

     <td width="14%" align="left"><pre><b>Shift: </b> 
5:30 PM - 9:00 PM 
<b>Meal:</b> 
- </pre></td> 


     <td width="14%" align="left"><pre>&nbsp;</pre></td> 

     <td width="14%" align="left"><pre>&nbsp;</pre></td> 

     <td width="14%" align="left"><pre>&nbsp;</pre></td> 

     <td width="14%" align="left"><pre><b>Shift: </b> 
2:00 PM - 9:15 PM 
<b>Meal:</b> 
5:45 PM - 6:30 PM</pre></td> 

     <td width="14%" align="left"><pre><b>Shift: </b> 
4:45 PM - 9:15 PM 
<b>Meal:</b> 
- </pre></td> 

     <td width="12%" align="center">14.5</td> 
     </tr> 

     <tr bgcolor="#FFFFFF"> 

     <td width="14%" align="center">0.0</td> 

     <td width="14%" align="center">3.5</td> 

     <td width="14%" align="center">0.0</td> 

     <td width="14%" align="center">0.0</td> 

     <td width="14%" align="center">0.0</td> 

     <td width="14%" align="center">6.5</td> 

     <td width="14%" align="center">4.5</td> 

     <td width="14%" align="center">Daily Hours</td> 
     </tr> 

    </table> 
    </td></tr> 

     <tr><td> 
     <table border="4" bordercolor="#2D73B9" cellpadding="2" cellspacing="2" width="100%"> 
     <tr bgcolor="#7C9BCF"> 
     <td width="12%" align="center"><b>Sunday</b></td> 
     <td width="12%" align="center"><b>Monday</b></td> 
     <td width="12%" align="center"><b>Tuesday</b></td> 
     <td width="12%" align="center"><b>Wednesday</b></td> 
     <td width="12%" align="center"><b>Thursday</b></td> 
     <td width="12%" align="center"><b>Friday</b></td> 
     <td width="12%" align="center"><b>Saturday</b></td> 
     <td rowspan="2" width="12%" align="center"><b>Total weekly Hours</b></td> 
     </tr> 

     <tr bgcolor="#7C9BCF"> 

     <td width="14%" align="center">2013-03-03</td> 

     <td width="14%" align="center">2013-03-04</td> 

     <td width="14%" align="center">2013-03-05</td> 

     <td width="14%" align="center">2013-03-06</td> 

     <td width="14%" align="center">2013-03-07</td> 

     <td width="14%" align="center">2013-03-08</td> 

     <td width="14%" align="center">2013-03-09</td> 

     </tr> 

     <tr bgcolor="#FFFFFF"> 

     <td width="14%" align="left"><pre>Sched Not Posted</pre></td> 

     <td width="14%" align="left"><pre>Sched Not Posted</pre></td> 

     <td width="14%" align="left"><pre>Sched Not Posted</pre></td> 

     <td width="14%" align="left"><pre>Sched Not Posted</pre></td> 

     <td width="14%" align="left"><pre>Sched Not Posted</pre></td> 

     <td width="14%" align="left"><pre>Sched Not Posted</pre></td> 

     <td width="14%" align="left"><pre>Sched Not Posted</pre></td> 

     <td width="12%" align="center">0.0</td> 
     </tr> 

     <tr bgcolor="#FFFFFF"> 

     <td width="14%" align="center">0.0</td> 

     <td width="14%" align="center">0.0</td> 

     <td width="14%" align="center">0.0</td> 

     <td width="14%" align="center">0.0</td> 

     <td width="14%" align="center">0.0</td> 

     <td width="14%" align="center">0.0</td> 

     <td width="14%" align="center">0.0</td> 

     <td width="14%" align="center">Daily Hours</td> 
     </tr> 

    </table> 
    </td></tr> 

     <tr><td> 
     <table border="4" bordercolor="#2D73B9" cellpadding="2" cellspacing="2" width="100%"> 
     <tr bgcolor="#7C9BCF"> 
     <td width="12%" align="center"><b>Sunday</b></td> 
     <td width="12%" align="center"><b>Monday</b></td> 
     <td width="12%" align="center"><b>Tuesday</b></td> 
     <td width="12%" align="center"><b>Wednesday</b></td> 
     <td width="12%" align="center"><b>Thursday</b></td> 
     <td width="12%" align="center"><b>Friday</b></td> 
     <td width="12%" align="center"><b>Saturday</b></td> 
     <td rowspan="2" width="12%" align="center"><b>Total weekly Hours</b></td> 
     </tr> 

     <tr bgcolor="#7C9BCF"> 

     <td width="14%" align="center">2013-03-10</td> 

     <td width="14%" align="center">2013-03-11</td> 

     <td width="14%" align="center">2013-03-12</td> 

     <td width="14%" align="center">2013-03-13</td> 

     <td width="14%" align="center">2013-03-14</td> 

     <td width="14%" align="center">2013-03-15</td> 

     <td width="14%" align="center">2013-03-16</td> 

     </tr> 

     <tr bgcolor="#FFFFFF"> 

     <td width="14%" align="left"><pre>Sched Not Posted</pre></td> 

     <td width="14%" align="left"><pre>Sched Not Posted</pre></td> 

     <td width="14%" align="left"><pre>Sched Not Posted</pre></td> 

     <td width="14%" align="left"><pre>Sched Not Posted</pre></td> 

     <td width="14%" align="left"><pre>Sched Not Posted</pre></td> 

     <td width="14%" align="left"><pre>Sched Not Posted</pre></td> 

     <td width="14%" align="left"><pre>Sched Not Posted</pre></td> 

     <td width="12%" align="center">0.0</td> 
     </tr> 

     <tr bgcolor="#FFFFFF"> 

     <td width="14%" align="center">0.0</td> 

     <td width="14%" align="center">0.0</td> 

     <td width="14%" align="center">0.0</td> 

     <td width="14%" align="center">0.0</td> 

     <td width="14%" align="center">0.0</td> 

     <td width="14%" align="center">0.0</td> 

     <td width="14%" align="center">0.0</td> 

     <td width="14%" align="center">Daily Hours</td> 
     </tr> 

    </table> 
    </td></tr> 

    <tr> 
    <td colspan="8" align="center"><b><font size="+1">Schedules may be subject to change based on business needs or demand</font></b></td> 
    </tr> 
    </table > 

     <p><br> 
     </p> 
     <p class="align_center" >  
      <input type=button value="Print this page" onClick="javascript:window.print();">  
      <input type=button value="Close This Window" onClick="javascript:window.close();"> 
     </p> 

    </div> 
    </body> 

</html> 
+3

在提供代碼或數據(如HTML)時,請將其降至必要的最低限度以展示/重現問題。除此之外,浪費我們的時間,並妨礙我們能夠幫助你。另外,你不會說你試圖完成什麼。 – 2013-02-25 03:18:57

回答

5

公告在你的XPath訪問器中,你需要tbody作爲路徑的一部分:

puts page.search('/html/body/div/table/tbody/tr[2]/td/table/tbody/tr[2]/td[2]').inspect 
puts post_page.parser.xpath('/html/body/div/table/tbody/tr[2]/td/table/tbody/tr[2]/td[2]').inspect 
puts post_page.parser.at_xpath('/html/body/div/table/tbody/tr[2]/td/table/tbody/tr[2]/td[2]').inspect 

HTML沒有tbody標記,導致查找失敗。

嘗試簡化您的訪問者。我通常從Nokogiri支持的CSS開始,然後,如果我不能到達那裏,我切換到XPath。你的里程可能會有所不同。

例如:

(rdb:1) puts doc.at('table table tr').to_html 

輸出:

<tr bgcolor="#7C9BCF"> 
<td width="12%" align="center"><b>Sunday</b></td> 
     <td width="12%" align="center"><b>Monday</b></td> 
     <td width="12%" align="center"><b>Tuesday</b></td> 
     <td width="12%" align="center"><b>Wednesday</b></td> 
     <td width="12%" align="center"><b>Thursday</b></td> 
     <td width="12%" align="center"><b>Friday</b></td> 
     <td width="12%" align="center"><b>Saturday</b></td> 
     <td rowspan="2" width="12%" align="center"><b>Total weekly Hours</b></td> 
     </tr> 

這是在列標題獲得了很多簡單的方法。

爲了獲得在第二行,你可以使用:

(rdb:1) puts doc.at('table table tr[2]').to_html 

它可以幫助您:

<tr bgcolor="#7C9BCF"> 
<td width="14%" align="center">2013-02-24</td> 
     <td width="14%" align="center">2013-02-25</td> 
     <td width="14%" align="center">2013-02-26</td> 
     <td width="14%" align="center">2013-02-27</td> 
     <td width="14%" align="center">2013-02-28</td> 
     <td width="14%" align="center">2013-03-01</td> 
     <td width="14%" align="center">2013-03-02</td> 
     </tr> 

要獲取單元格的內容,你可以使用:

(rdb:1) puts doc.search('table table tr[2] td').map(&:text) 

哪退貨:

2013-02-24 
2013-02-25 
2013-02-26 
2013-02-27 
2013-02-28 
2013-03-01 
2013-03-02 
2013-03-03 
2013-03-04 
2013-03-05 
2013-03-06 
2013-03-07 
2013-03-08 
2013-03-09 
2013-03-10 
2013-03-11 
2013-03-12 
2013-03-13 
2013-03-14 
2013-03-15 
2013-03-16 

請注意這是如何返回兩個表的標題。爲了限制它到第一個表格,我們可以使用at而不是searchat返回第一個匹配節點,其中search返回一個NodeSet,就像一個數組。此外,search查看整個文檔查找所有匹配,不像at的行爲。

這個代碼是找到的第一個表的第二行,然後走嵌入式單元:

 
(rdb:1) puts doc.at('table table tr[2]').search('td').map(&:text) 
2013-02-24 
2013-02-25 
2013-02-26 
2013-02-27 
2013-02-28 
2013-03-01 
2013-03-02 

這是更簡單,更容易理解和維護。

+0

「使用源碼,盧克!」尤其是像HTML一樣被濫用的東西。 – 2013-02-25 03:33:16