使用Selenium和Python保存表格

3
我正在尝试使用Python中的Selenium来存储表格内容。以下是我的脚本:
import sys
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://testsite.com")

value = selenium.getTable("table_id_10")

print value

driver.close()

这将打开我感兴趣的网页,然后应该保存我想要的表格内容。我看到了这个问题中使用了browser.get_table()的语法,但那个程序的开头是browser=Selenium(...),我不明白。我不确定我应该使用什么语法,因为selenium.getTable("table_id_10")是错误的。

编辑:

我包含了一个我正在使用的表格的html片段:

<table class="datatable" cellspacing="0" rules="all" border="1" id="table_id_10" style="width:70%;border-collapse:collapse;">
    <caption>
        <span class="captioninformation right"><a href="Services.aspx" class="functionlink">Return to Services</a></span>Data
    </caption><tr>
        <th scope="col">Read Date</th><th class="numericdataheader" scope="col">Days</th><th class="numericdataheader" scope="col">Values</th>

    </tr><tr>
        <td>10/15/2011</td><td class="numericdata">92</td><td class="numericdata">37</td>
    </tr><tr class="alternaterows">
        <td>7/15/2011</td><td class="numericdata">91</td><td class="numericdata">27</td>
    </tr><tr>
        <td>4/15/2011</td><td class="numericdata">90</td><td class="numericdata">25</td>    
</table>

如果你想进行网页抓取(我认为这就是你要做的事情),你可能还想看看 mechanize。我过去用过它,真的很喜欢,但不幸的是文档相当缺乏,使用起来有点困难。只是一个想法,希望不会太离题。 - Naftuli Kay
@TKKocheran 我对爬虫很感兴趣,尽管在这种情况下只有一个表格。我也可以保存HTML页面并稍后单独解析它。 - djq
1
你可能想要考虑使用 mechanize。一旦你掌握了它,mechanize 在处理事情方面非常强大。我曾经编写过一个脚本,可以登录我的银行账户,回答一个安全问题,然后使用银行应用程序中可用的表单获取我的财务数据。很有趣的东西。 - Naftuli Kay
1个回答

17

旧版 Selenium RC API 包含一个 get_table 方法:

In [14]: sel=selenium.selenium("localhost",4444,"*firefox", "http://www.google.com/webhp")
In [19]: sel.get_table?
Type:       instancemethod
Base Class: <type 'instancemethod'>
String Form:    <bound method selenium.get_table of <selenium.selenium.selenium object at 0xb728304c>>
Namespace:  Interactive
File:       /usr/local/lib/python2.7/dist-packages/selenium/selenium.py
Definition: sel.get_table(self, tableCellAddress)
Docstring:
    Gets the text from a cell of a table. The cellAddress syntax
    tableLocator.row.column, where row and column start at 0.

    'tableCellAddress' is a cell address, e.g. "foo.1.4"

由于您正在使用较新的Webdriver(也称为Selenium 2)API,因此该代码不适用。


可能尝试使用以下代码:

import selenium.webdriver as webdriver
import contextlib

@contextlib.contextmanager
def quitting(thing):
    yield thing
    thing.close()
    thing.quit()

with quitting(webdriver.Firefox()) as driver:
    driver.get(url)
    data = []
    for tr in driver.find_elements_by_xpath('//table[@id="table_id_10"]//tr'):
        tds = tr.find_elements_by_tag_name('td')
        if tds: 
            data.append([td.text for td in tds])
print(data)
# [[u'10/15/2011', u'92', u'37'], [u'7/15/2011', u'91', u'27'], [u'4/15/2011', u'90', u'25']]

感谢您解释为什么 get_table 没有起作用。我添加了一个 HTML 表格的片段 - 我正在尝试从单元格中提取所有值。 - djq
+1 包含 contextlib。我之前不知道这个东西,现在才发现。我得把所有的脚本都改成这种方式工作 :D - RattleyCooper
1
@DuckPuncher:对于某些版本的webdriver,您可能需要同时调用closequit(http://seleniumsimplified.com/2013/08/faq-why-has-my-firefox-selenium-webdriver-browser-not-closed/)。我已经更新了上面的代码,展示了一个可以同时执行两者功能的上下文管理器。 - unutbu
@unutbu,太棒了! - RattleyCooper

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接