mkt*_*012 16 html python xpath lxml html-table
我有一个类似于以下的html文档:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml">
<div id="Symbols" class="cb">
<table class="quotes">
<tr><th>Code</th><th>Name</th>
<th style="text-align:right;">High</th>
<th style="text-align:right;">Low</th>
</tr>
<tr class="ro" onclick="location.href='/xyz.com/A.htm';" style="color:red;">
<td><a href="/xyz.com/A.htm" title="Display,A">A</a></td>
<td>A Inc.</td>
<td align="right">45.44</td>
<td align="right">44.26</td>
<tr class="re" onclick="location.href='/xyz.com/B.htm';" style="color:red;">
<td><a href="/xyz.com/B.htm" title="Display,B">B</a></td>
<td>B Inc.</td>
<td align="right">18.29</td>
<td align="right">17.92</td>
</div></html>
Run Code Online (Sandbox Code Playgroud)
我需要code/name/high/low从表中提取信息.
我使用了Stack Over Flow中类似示例中的以下代码:
#############################
import urllib2
from lxml import html, etree
webpg = urllib2.urlopen(http://www.eoddata.com/stocklist/NYSE/A.htm).read()
table = html.fromstring(webpg)
for row in table.xpath('//table[@class="quotes"]/tbody/tr'):
for column in row.xpath('./th[position()>0]/text() | ./td[position()=1]/a/text() | ./td[position()>1]/text()'):
print column.strip(),
print
#############################
Run Code Online (Sandbox Code Playgroud)
我没有得到任何输出.我必须将第一个循环xpath更改table.xpath('//tr')为table.xpath('//table[@class="quotes"]/tbody/tr')
我只是不明白为什么xpath('//table[@class="quotes"]/tbody/tr')不行.
sam*_*ias 40
您可能正在查看Firebug中的HTML,对吗?<tbody>当文档中不存在隐式标记时,浏览器将插入隐式标记.lxml库将仅处理原始HTML字符串中存在的标记.
省略XPath中的tbody级别.例如,这有效:
tree = lxml.html.fromstring(raw_html)
tree.xpath('//table[@class="quotes"]/tr')
[<Element tr at 1014206d0>, <Element tr at 101420738>, <Element tr at 1014207a0>]
Run Code Online (Sandbox Code Playgroud)