2017-07-20 2 views
0

ich für ein paar Stunden habe nun versucht, für vergeblich einen Text aus einer bestimmten Zelle in der folgenden Tabelle zu entnehmen:kann nicht innerhalb einer HTML-Tabelle auf Zellentext zugreifen (Selenium, Python)

<tbody class="table-body"> 
    <tr class=" " data-blah="25293454534534513" data-currency="1"> 
     <td class="action-cell no-sort"> 
     <a href="" class="buy-btn tooltip" data-tooltip="Buy the bond"></a><a href="" class="sell-btn tooltip" data-tooltip="Sell the bond"></a> 
     </td> 
     <td class="col1 id"> 
     <a class="alert-ico " data-tooltip=""></a> 
     <a class="isin-btn " data-tooltip="" id="isin" data-portfolioid="2423424" data-status="0">US3</a> 
     </td> 
     <td class="col2 name hide">4%</td> 
     <td class="col9 colNo.9" title="Bid: 101.23; Mid: 101.28; Ask: 101.33; 
     Liquidity Score: -*/5*; Merit: -/4;" data-bprice="101.28" data-uprice="101.28">101.28<span class="estim-star">*</span></td> 
     <td class="col10 price_change" nowrap="" data-sort="0.02"><span class="positive-change">0.02%</span><span class="change-sign positive-change">↑</span></td> 
     <td class="col11 yield yield-val" title="" data-sort="3.33" data-byield="3.33" data-uyield="3.34%">3.33%</td> 
     <td class="col12 purchase_price" data-bprice="101.28" data-uprice="101.28" data-sort="101.28"><input type="text" name="purchase_price" class="positive-num-only default" value="101.28"></td> 
     <td class="col13 margin_bond" data-bond="sec" data-sort="0"><input type="text" name="margin_bond" maxlength="3" class="positive-num-only default" value="0"></td> 
    </tr> 
</tbody> 

I‘ Ich versuche, einen Text aus der Spalte 'Preisänderung' (Spalte 10) mit lxml.html zu extrahieren, was mir erlaubt, Daten aus großen Tabellen in Sekundenschnelle zu extrahieren. Ich mache es wie folgt aus:

import lxml.html 
import pandas as pd 
root = lxml.html.fromstring(self.driver.page_source) 
data = [] 
for row in root.xpath('.//*[@id=\'main\']/div[5]/div[2]/table/tbody/tr'): 
    cells = row.xpath('.//td/text()') 

So gelang es mir den ganzen Tisch, so zu extrahieren, und ich weiß, dass die einzige Ausnahme Spalte 10 (‚Preisänderung‘) und versuchte, die folgenden und es kehrte die leere Zeichenfolge (""):

  1. row.xpath ('.// tr [1]/td [11] [@ data-sort]/text()')

  2. Reihe. xpath ('.//[@ id =' main ']/div [5]/div [2]/Tabelle/tbody/tr [1]/td [11]/span/text()')

  3. row.xpath ('.//*[@ id =' main ']/div [5]/div [2]/tabelle/tbody/tr [1]/td [11]/text()')

ich will nicht, um den Text extrahieren mit WebElement aber nur mit lxml.html Bibliothek

Thank you!

+0

was tut gibt diese 'root.xpath (" // Tabelle [@ class = 'table-body'] // td [enthält (Klasse, 'col10')])/span' den Wert überprüfen in Watchlist oder Debugger: Welchen Text enthält es? –

Antwort

0

Es gibt zwei Probleme

  1. Es gibt insgesamt 7 tds und nicht , die td Sie intersted ist 5 und nicht 11
  2. die td Sie in intersted zwei Spanne und Sie geben nicht an, für welche Spanne Sie sich interessieren.

Dieser Code funktioniert einwandfrei.

html_code = """ 
<tbody class="table-body"> 
    <tr class=" " data-blah="25293454534534513" data-currency="1"> 
     <td class="action-cell no-sort"> 
     <a href="" class="buy-btn tooltip" data-tooltip="Buy the bond"></a><a href="" class="sell-btn tooltip" data-tooltip="Sell the bond"></a> 
     </td> 
     <td class="col1 id"> 
     <a class="alert-ico " data-tooltip=""></a> 
     <a class="isin-btn " data-tooltip="" id="isin" data-portfolioid="2423424" data-status="0">US3</a> 
     </td> 
     <td class="col2 name hide">4%</td> 
     <td class="col9 colNo.9" title="Bid: 101.23; Mid: 101.28; Ask: 101.33; 
     Liquidity Score: -*/5*; Merit: -/4;" data-bprice="101.28" data-uprice="101.28">101.28<span class="estim-star">*</span></td> 
     <td class="col10 price_change" nowrap="" data-sort="0.02"> 
     <span class="positive-change">0.02%</span> 
     <span class="change-sign positive-change">↑</span></td> 
     <td class="col11 yield yield-val" title="" data-sort="3.33" data-byield="3.33" data-uyield="3.34%">3.33%</td> 
     <td class="col12 purchase_price" data-bprice="101.28" data-uprice="101.28" data-sort="101.28"><input type="text" name="purchase_price" class="positive-num-only default" value="101.28"></td> 
     <td class="col13 margin_bond" data-bond="sec" data-sort="0"><input type="text" name="margin_bond" maxlength="3" class="positive-num-only default" value="0"></td> 
    </tr> 
</tbody> 
""" 


tree = html.fromstring(html_code) 

print "purchase price is %s" % tree.xpath(".//td[contains(@class,'col10')]/span[1]/text()")[0] 
print "purchase price is %s" % tree.xpath(".//td[5]/span[1]/text()")[0] 
Verwandte Themen