【问题标题】:Getting data from this table html python从此表中获取数据 html python
【发布时间】:2017-09-23 20:20:58
【问题描述】:

我想从这个显示货币汇率的表格中提取数据。

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

我尝试过这种方法,但它不起作用

      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

这是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>

我也尝试过 python selenium 方法,但是我可以得到每个货币的汇率,但不能得到名称

             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)

【问题讨论】:

  • 哪个名字?预期的输出是什么?
  • 输出 1.146 欧元
  • 它的意思是在一行中以这种格式输出整个表格
  • 欧元 1.146 美元 - 1.2536 澳元 - 1.6647
  • 你的html中的Euro 1.146 US Dollar - 1.2536 Australian Dollar - 1.6647在哪里?

标签: python html selenium web driver


【解决方案1】:

如果您只是想获取汇率,那么最好使用 api,请参阅this question。网页抓取使您容易受到目标网页更改的影响而破坏您的代码。

如果抓取是您的目标,您只需要重用您的 selenium 方法,但搜索“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]) )

【讨论】:

    【解决方案2】:

    通过分析页面的结构,很明显你必须逐行分析,你必须选择你感兴趣的列组件。

    对于每一行,使用find_element_by_tag_namefind_element_by_class_name 提取您感兴趣的两个元素

    (这里有文档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
    ... 
    

    【讨论】:

      猜你喜欢
      • 2013-01-08
      • 2013-12-16
      • 2012-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-13
      • 2020-04-02
      相关资源
      最近更新 更多