2016-03-22 55 views
0

這是一段HTML代碼: -XPath選擇不返回匹配

source1 = ' 

    <tr> 
     <td bgcolor="#ffffff"><font face="Tahoma" size="2">Gemara</font></td> 
     <td bgcolor="#ffffff"><font face="Tahoma" size="2">Kiddushin</font></td> 
     <td bgcolor="#ffffff"><font face="Tahoma" size="2">Morning</font></td> 

     <td bgcolor="#ffffff"><font face="Tahoma" size="2">12-04-2104</font></td> 

     <td colspan=2 bgcolor="#ffffff" nowrap="nowrap"><font face="Tahoma" size="2"> 
     <a href="#" onClick="listen('05-115-08-2104-12-04.mp3')"><img src="images/play_audio.gif" border="0"></a> 
     <a href="#" onClick="mydownload('05-115-08-2104-12-04.mp3')"><img src="images/download.gif" border="0"></a> 
     </td> 
     <!-- <td bgcolor="#ffffff" nowrap="nowrap"><font face="Tahoma" size="2"> 

     <a href="http://mgr.uvault.com/yadavraham/media//05-115-08-2104-12-04.mp3">Download</a> 
     </td> 
     --> 
    </tr> 
' 

我能夠從HTML解析的所有數據,只有Mp3的文件名解析不返回任何值

請參閱下面我的代碼:

from lxml import html 
source2 = html.fromstring(str(source1)) 

Category = source2.xpath('//tr[1]//td[@bgcolor="#ffffff"][1]//text()') 
Book = source2.xpath('//tr[1]//td[@bgcolor="#ffffff"][2]//text()') 
Section = source2.xpath('//tr[1]//td[@bgcolor="#ffffff"][3]//text()') 
Date = source2.xpath('//tr[1]//td[@bgcolor="#ffffff"][4]//text()') 
Mp3filename = source2.xpath('//tr[1]//td[@colspan=2]//a[1]//@onClick') 
print Category, Book, Section, Date, Mp3filename 

Mp3filename變量返回NULL值。我的Xapth查詢是否正確?

+2

您錯過了''標籤。 –

+0

@KlausD。這是不需要的,路徑被設置爲絕對// a [1]。如果我將// [1] // @ onClick'替換爲// a [1] // @ href',它確實返回'#' –

回答

1

它看起來像lxml.html屬性名轉化爲小寫(在Python 2.7測試,從HTML的問題拷貝粘貼,沒有變化):

raw= '''<tr> 
            <td bgcolor="#ffffff"><font face="Tahoma" size="2">Gemara</font></td> 
            <td bgcolor="#ffffff"><font face="Tahoma" size="2">Kiddushin</font></td> 
            <td bgcolor="#ffffff"><font face="Tahoma" size="2">Morning</font></td> 

            <td bgcolor="#ffffff"><font face="Tahoma" size="2">12-04-2104</font></td> 

            <td colspan=2 bgcolor="#ffffff" nowrap="nowrap"><font face="Tahoma" size="2"> 
            <a href="#" onClick="listen('05-115-08-2104-12-04.mp3')"><img src="images/play_audio.gif" border="0"></a> 
            <a href="#" onClick="mydownload('05-115-08-2104-12-04.mp3')"><img src="images/download.gif" border="0"></a> 
            </td> 
            <!-- <td bgcolor="#ffffff" nowrap="nowrap"><font face="Tahoma" Size="2"> 

            <a href="http://mgr.uvault.com/yadavraham/media//05-115-08-2104-12-04.mp3">Download</a> 
            </td> 
            --> 
            </tr>''' 

from lxml import html 
source2 = html.fromstring(raw) 

Mp3filename = source2.xpath('//tr[1]//td[@colspan=2]//a[1]') 
print html.tostring(Mp3filename[0]) 
# output : 
# <a href="#" onclick="listen('05-115-08-2104-12-04.mp3')"><img src="images/play_audio.gif" border="0"></a> 
#    ^notice that the attribute name changed to lower-case 

所以我建議使用較低試試case @onclick在您的XPath中:

Mp3filename = source2.xpath('//tr[1]//td[@colspan=2]//a[1]/@onclick') 
+1

非常棒的人,我從不知道lxml將屬性nodeset轉換爲小寫。這被稱爲有經驗的男人詳細的眼睛。 我做到了這一點 - Mp3filename = source2.xpath('// tr [1] // td [@ colspan = 2] // font [@face =「Tahoma」] // a [1] [@ href = 「#」] // @ onclick'); Mp3filename = str(Mp3filename [0])。replace(「listen('」,'')。replace(「')」,'')。strip() –

0

首先修復您的HTML,因此它是有效的xml。

你錯過了最後<td><font>的結束標記。因此,XPath將不會在其下找到任何有效的xml。

+0

html.fromstring(source),因此不需要將html轉換爲任何xml模式。無論如何感謝您的建議。 –