【问题标题】:Find table by known cell and retrieve contents of cell before and after通过已知单元格查找表格并检索单元格前后的内容
【发布时间】:2015-04-20 16:33:44
【问题描述】:

我正在使用一些快速脚本来读取表格数据。页面上有多个表,它们似乎是用 ajax 动态加载的,没有 id 可以使用 xpath。我需要日期之前的单元格和单元格中的文本<td><span style="">First Last</span></td> 之后的单元格,我知道这些单元格将被修复。我需要识别的有问题的表是。

<table cellspacing="0" class="collections">
    <thead>
        <tr>
            <td colspan="4" class="actionsWrapper">
                <table cellpadding="0" cellspacing="0" width="100%">
                    <thead></thead>
                    <tbody>
                        <tr>
                            <td><span style="display: none;"><b>Current Group: </b> <span><select class="standard_input"></select></span>&nbsp;&nbsp; </span><span>(<font color="red"><span>2</span></font> Notes)</span><span style="display: none;">&nbsp;&nbsp;&nbsp;<a href="javascript: void(null)"><font size="-2">Edit Group</font></a> |  <span><a href="group_manager.php?type=12"><font id="create_group" size="-2">Create Group</font></a></span></span></td>
                            <td>
                                <div style="display: none;"><img src="include/images/loading_page.gif" height="70%"> <span style="font-size: .8em; font-weight: bold;">Retrieving Data...</span></div>
                            </td>
                            <td class="searchWrapper">
                                <table cellpadding="0" cellspacing="0">
                                    <thead></thead>
                                    <tbody>
                                        <tr>
                                            <td><input type="TEXT" class="keyword icon magnifying-glass unfocused"></td>
                                        </tr>
                                        <tr>
                                            <td><span id="notesWrapper" style="display: none;"><label for="notesToggle">Search notes</label><input type="CHECKBOX" class="inpt_checkbox standard_input" id="notesToggle"></span></td>
                                        </tr>
                                    </tbody>
                                    <tfoot></tfoot>
                                </table>
                            </td>
                        </tr>
                    </tbody>
                    <tfoot></tfoot>
                </table>
            </td>
        </tr>
    </thead>
    <tbody>
        <tr class="header">
            <td class="utils"></td>
            <td class="pointer bold" style="width: 200px;">Date</td>
            <td class="pointer bold">Note</td>
            <td class="pointer bold openArrow">Author</td>
        </tr>
        <tr class="data" style="cursor: default;">
            <td class="actions"><input type="CHECKBOX" class="checkbox" style="display: none;"><a class="icon trashcan" title="Delete Note">Delete Note</a></td>
            <td style="width: 200px;"><span style="">8/24/2011 12:00 PM</span></td>
            <td><span style="">First Last</span></td>
            <td><span style="">No answer - went to answering machine</span></td>
        </tr>
        <tr class="detailWrapper" style="display: none;"></tr>
        <tr class="data" style="cursor: default;">
            <td class="actions"><input type="CHECKBOX" class="checkbox" style="display: none;"><a class="icon trashcan" title="Delete Note">Delete Note</a></td>
            <td style="width: 200px;"><span style="">8/26/2011 11:08 AM</span></td>
            <td><span style="">First Last</span></td>
            <td><span style="">Philip hardly comes into this store</span></td>
        </tr>
        <tr class="detailWrapper" style="display: none;"></tr>
    </tbody>
    <tfoot>
        <tr style="display: none;"></tr>
        <tr>
            <td colspan="4">
                <table width="100%" style="margin-top:5px;">
                    <tbody>
                        <tr>
                            <td align="left">
                                <div class="navigationPanel" style="display: none;"><a style="color: rgb(156, 156, 155); cursor: default;">&lt;&lt;</a>  <a style="color: rgb(156, 156, 155); cursor: default;">&lt;</a>  Page: <input type="TEXT" class="inpt_text standard_input" size="2"><span> of 1 </span>  <a style="cursor: default; color: rgb(156, 156, 155);">&gt;</a>  <a style="cursor: default; color: rgb(156, 156, 155);">&gt;&gt;</a></div>
                            </td>
                            <td align="right">
                                Entries Per Page: 
                                <select>
                                    <option value="10" selected="">10</option>
                                    <option value="25">25</option>
                                    <option value="50">50</option>
                                </select>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </td>
        </tr>
        <tr>
            <td colspan="4" align="left" style="margin-left: 2px;"><textarea style="width: 70%;"></textarea><input type="BUTTON" class="btn2" value="Add" style="width: 50px; margin-left: 10px;"></td>
        </tr>
        <tr style="display: none;">
            <td colspan="4" class="groupActionsWrapper">
                <div class="stepbar">Group Actions</div>
                <br>
                <table width="100%">
                    <tbody>
                        <tr>
                            <td style="padding-top:2px;width: 50px" align="right" valign="top">With </td>
                            <td style="width: 100px;" align="left" valign="top">
                                <select>
                                    <option value="0">Selected</option>
                                    <option value="1">All in group</option>
                                </select>
                            </td>
                            <td></td>
                        </tr>
                    </tbody>
                </table>
            </td>
        </tr>
    </tfoot>
</table>

【问题讨论】:

  • 这张桌子的独特之处是什么?
  • @tommy.carstensen 只是它包含具有 First Last 的单元格
  • 如果您可以将整个页面保存在内存中,我会为此使用正则表达式。或者逐行搜索并通过将所有行保留在内存中或仅将上一行保留在内存中来打印上一行和下一行。

标签: python html selenium selenium-webdriver


【解决方案1】:

如果您不熟悉模块re 和/或html.parser,这里是其中一种方法:

line_prev = ''
with open('29740695.htm') as f:
    for line in f:
        if line != '            <td><span style="">First Last</span></td>\n':
            line_prev = line
            continue
        print(line_prev)
        print(f.readline())

【讨论】:

  • 感谢@tommy.carstensen,如果真正的 html 被格式化,这可能会起作用。我只使用在线 html 格式化程序手动格式化它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-30
  • 2015-11-15
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 2020-04-20
相关资源
最近更新 更多