这可能只是一个语法问题。
我不清楚如何只匹配 id 以 rowId_ 开头的表行
agent = Mechanize.new
pageC1 = agent.get("/customStrategyScreener!list.action")
该表具有 class=tableCellDT。
pageC1.search('table.tableCellDT tr[@id=rowId_]') # parses OK but returns 0 rows since rowId_ is not matched exactly.
pageC1.search('table.tableCellDT tr[@id=rowId_*]') # Throws an error since * is not treated like a wildcard string match
示例 HTML:
<table id="row" cellpadding="5" class="tableCellDT" cellspacing="1">
<thead>
<tr>
<th class="tableHeaderDT">#</th>
<th class="tableHeaderDT sortable">
<a href="?d-16544-s=1&d-16544-o=2&d-16544-p=1">Screener</a></th>
<th class="tableHeaderDT sortable">
<a href="?d-16544-s=2&d-16544-o=2&d-16544-p=1">Strategy</a></th>
<th class="tableHeaderDT"> </th></tr></thead>
<tbody>
<tr id="rowId_BullPut" class="odd">
<td> 1 </td>
<td> Bull</td>
<td></td>
<td><a href="link1?model.itemId=2262">Edit</a>
<a href="javascript:deleteScreener('link2?model.itemId=2262');">Delete</a>
<a href="link3?model.itemId=2262&amp;model.source=list">View</a>
</td></tr>
笔记
pageC1是一个Mechanize::Page
对象,而不是Nokogiri
任何东西。抱歉,一开始不清楚。Mechanize::Page 没有#css 或#xpath 方法,但可以从中提取Nokogiri 文档(无论如何在内部使用)。