2017-04-26 3 views
1

この表から通貨レートを示すデータを抽出したいと思います。このテーブルからデータを取得するhtml python

私はこのアプローチを試してみましたが、そのこれは私もしかし、私は得ることができますPythonのセレンのアプローチを試してみましたが、HTML

</td> 

        <td valign="top" class="OuterProdCell test"> 

           <table class="ProductCell"> 
            <tr> 
            <td class="rateCountryFlag"> 
             <ul id="prodImages"> 
              <li> 
               <a href="/travel-money/buy-chilean-peso" title="Buy Chilean Peso" class="flags chilean-peso" ></a> 
              </li> 
             </ul> 
            </td> 

            <td class="ratesName"> 
            <a href="/travel-money/buy-chilean-peso" title="Buy Chilean Peso"> 
            Chilean Peso</a> 
            </td> 

            <td class="ratesClass"> 
            <a class="orderText" href="/travel-money/buy-chilean-peso" title="Buy Chilean Peso">774.8540</a> 
            </td> 
            <td class="orderNow">           
             <ul id="prodImages"> 
              <li> 
               <a class="reserveNow" href="/travel-money/buy-chilean-peso" title="Buy Chilean Peso">Order<br/>now</a> 
              </li> 
              <li> 
               <a href="/travel-money/buy-chilean-peso" title="Buy Chilean Peso" class="flags arrowGreen" ></a> 
              </li> 
             </ul> 
            </td> 
            </tr> 
           </table> 

ある

 table_id = driver.find_element(By.ID, 
    'data_configuration_feeds_ct_fields_body0') 
     rows = table_id.find_elements(By.TAG_NAME, "tr") # get all of the 
     rows in the table 
     for row in rows: 

     col = row.find_elements(By.TAG_NAME, "td")[1] #note: index start from 
     0, 1 is col 2 
     print(col.text) #prints text from the element 

を働いていない訪問https://www.iceplc.com/travel-money/exchange-rates

それぞれの通貨で、名前は含まれていません。

   driver.get("https://www.iceplc.com/travel-money/exchange- 
      rates") 
      rates = driver.find_elements_by_class_name("ratesClass") 

      for rate in rates: 
      print(rate.text) 
+0

?予想される出力は何ですか? –

+0

出力ユーロ1.146 – xys234

+0

この形式でテーブル全体を出力することを意味します。 – xys234

答えて

1

換算レートを取得しようとしている場合は、APIを使用する方が良いでしょう。this questionを参照してください。 Webスクレイピングでは、ターゲットWebページの変更によりコードが壊れる可能性があります。

掻き集めを目標にしている場合は、セレンのアプローチを再利用するだけで、「ratesName」クラスを検索する必要があります。例えば

driver.get("https://www.iceplc.com/travel-money/exchange-rates") 
rates.append((driver.find_elements_by_class_name("ratesName"), driver.find_elements_by_class_name("ratesClass"))) 

for rate in rates: 
print("Name: %s, Rate: %s" % (rate[0], rate[1])) 
1

ページの構造を分析することにより、あなたが行単位で解析する必要があり、あなたが興味を持っている列のコンポーネントを選択する必要があることは明らかです。あなたはfind_element_by_tag_namefind_element_by_class_name

を使用して、興味を持っている2つの要素を抽出する行ごとに

(ここではドキュメントhttp://selenium-python.readthedocs.io/locating-elements.html

driver.get("https://www.iceplc.com/travel-money/exchange-rates") 
rates=driver.find_elements_by_tag_name('tr') 
for i in rates: 
     print i.find_element_by_class_name('ratesName').text, i.find_element_by_class_name('ratesClass').text 

出力は次のとおりです。

US - Dollar 1.2536 
Croatia - Kuna 8.3997 
Canada - Dollar 1.7006 
Australia - Dollar 1.6647 
Euro - 1.1469 
...