1

I would like to extract data from this table which shows the currency rates.

Visit https://www.iceplc.com/travel-money/exchange-rates

I have tried this approach but its not working

      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

This is the 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>

I have also tried the python selenium approach however I can get the currency rate of each one but not the name

             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)
5
  • which name? What is the expected output? Commented Apr 26, 2017 at 12:29
  • outputs Euro 1.146 Commented Apr 26, 2017 at 12:31
  • Its meant to output the whole table in this format in a line going down Commented Apr 26, 2017 at 12:31
  • Euro 1.146 US Dollar - 1.2536 Australian Dollar - 1.6647 Commented Apr 26, 2017 at 12:32
  • And where is Euro 1.146 US Dollar - 1.2536 Australian Dollar - 1.6647 in your html? Commented Apr 26, 2017 at 12:34

2 Answers 2

1

If you're just trying to get the exchange rates then you would be better off using api, see this question. Web scraping leaves you vulnerable to changes in the target webpage breaking your code.

If scraping is your goal though, you just need to reuse your selenium approach but search for the "ratesName" class.

For example:

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]) )
Sign up to request clarification or add additional context in comments.

Comments

1

By analyzing the structure of the page it is clear that you have to analyze row by row and you have to select the column component you are interested.

For each row extracting the two elements you are interested by using the find_element_by_tag_name and find_element_by_class_name

(documentation here 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

Output is:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.