2011-11-15 23 views
2

私はPythonでSeleniumを使ってテーブルの内容を保存しようとしています。次のように私のスクリプトは次のとおりです。SeleniumとPythonを使ってテーブルを保存する

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() 

これは、私が興味を持ってWebページを開き、その後、私が欲しいテーブルの内容を保存する必要があります。このquestionの構文は、browser.get_table()を使用していますが、そのプログラムの最初は、私が理解できなかったbrowser=Selenium(...)で始まります。 selenium.getTable("table_id_10")としてどのような構文を使用するべきかわかりません。


EDIT:

私は私が使用しているテーブルの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> 
+0

を使用すると、Web-こすり(私はそれはあなたがやっていることだと思います)に探しているなら、あなたも見たいかもしれません[機械化] (http://wwwsearch.sourceforge.net/mechanize/)。私は過去にそれを使用していましたが、本当に好きでしたが、残念ながらドキュメントにはかなりの欠点があり、使用するのは少し難しいです。ちょうど考え、それはあまりにも話題ではないことを願っています。 –

+0

@TKKocheran私は掻き取りに興味がありますが、この場合は1つのテーブルです。私はまた、HTMLページを保存し、後でそれを別々に解析することもできます。 – djq

+1

次に、mechanizeを使用して調べることができます。一度あなたがそれのハングアップを取得し、機械化は物事を行う上で元気なです。私はかつて私の銀行口座にログインし、セキュリティに関する質問に答えるスクリプトを書いた後、銀行業務アプリケーションで利用可能な書式を使って私の財務データを取りに行きます。面白いもの。 –

答えて

15

古いの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']] 
+0

'get_table'がうまくいかなかった理由を説明してくれてありがとう。私はhtmlテーブルのスニペットを追加しました - 私は、セルからすべての値を引き出そうとしています。 – djq

+0

ありがとうございました!それは完全に機能します。 – djq

+0

+1は 'contextlib 'を含んでいます。私はこれが今までのことであるとは気づかなかった。私はすべてのスクリプトをこのように動作させる必要があります:D – DuckPuncher

関連する問題