xpath 选择器不返回匹配项
xpath selectors not returning a match
这是一段 HTML 代码:-
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 查询正确吗?
看起来 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
所以我建议尝试在您的 XPath 中使用小写 @onclick
:
Mp3filename = source2.xpath('//tr[1]//td[@colspan=2]//a[1]/@onclick')
首先修复您的 HTML,使其有效 xml。
您缺少最后一个 <td>
中 <font>
的结束标记。因此,XPath 不会在其下方找到任何有效的 xml。
这是一段 HTML 代码:-
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 查询正确吗?
看起来 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
所以我建议尝试在您的 XPath 中使用小写 @onclick
:
Mp3filename = source2.xpath('//tr[1]//td[@colspan=2]//a[1]/@onclick')
首先修复您的 HTML,使其有效 xml。
您缺少最后一个 <td>
中 <font>
的结束标记。因此,XPath 不会在其下方找到任何有效的 xml。