【问题标题】:.XPath for items under a certain td.XPath 用于特定 td 下的项目
【发布时间】:2019-10-17 15:55:47
【问题描述】:

我正在尝试抓取网站 (https://na.op.gg/champion/statistics) 以使用 xpath 获取哪些冠军的胜率最高,我可以使用它来做到这一点:

champion = tree.xpath('//div[@class="champion-index-table__name"]/text()')

但是,我意识到我想要获取的名称位于一张表上,该表的大小会根据当前游戏元数据而变化,所以我只想抓取属于特定类别的名称,所以我不会有任何当表中的冠军数量发生变化时,稍后会出现问题。该网站将它们分别放在不同的“层”下,如下所示:

<tbody class="tabItem champion-trend-tier-TOP" style="display: table-row-group;"> 
<tr>
<td class="champion-index-table__cell champion-index-table__cell--rank">1</td>
                                                        <td class="champion-index-table__cell champion-index-table__cell--change champion-index-table__cell--change--stay">
                                                                    <img src="//opgg-static.akamaized.net/images/site/champion/icon-championtier-stay.png" alt="">
                                                                0
                            </td>
                            <td class="champion-index-table__cell champion-index-table__cell--image">
                                <a href="/champion/garen/statistics/top"><i class="__sprite __spc32 __spc32-32"></i></a>
                            </td>
                            <td class="champion-index-table__cell champion-index-table__cell--champion">
                                <a href="/champion/garen/statistics/top">
                                    <div class="champion-index-table__name">Garen</div>
                                    <div class="champion-index-table__position">
                                                                                    Top, Middle                                                                         </div>
                                </a>
                            </td>
                            <td class="champion-index-table__cell champion-index-table__cell--value">53.12%</td>
                            <td class="champion-index-table__cell champion-index-table__cell--value">16.96%</td>
                            <td class="champion-index-table__cell champion-index-table__cell--value">
                                <img src="//opgg-static.akamaized.net/images/site/champion/icon-champtier-1.png" alt="">
</td>
                        </tr>
<tr> 

然后下一个去

&lt;tbody class="tabItem champion-trend-tier-JUNGLE" style="display: table-row-group;"&gt;

所以,我已经尝试过了,但它除了 [] 什么都没有输出。 希望我的问题是有道理的。

championtop = tree.xpath('//div/table/tbody/tr//td[4][@class="champion-index-table__name"]/text()')

【问题讨论】:

    标签: python html xpath


    【解决方案1】:

    我可以通过做事来实现我的目标

    champion = tree.xpath('//table[@class = "champion-index-table tabItems"]/tbody[@class="tabItem champion-trend-tier-TOP"]/tr/td[4]/a/div[1]

    感谢阅读!

    【讨论】:

      【解决方案2】:

      你可能会遍历所有的代码行,我相信你会得到你的答案。 这就是您可以轻松可靠地在 xpath 中定位元素的方法

      初始设置

      from selenium import webdriver
      wb = webdriver.Chrome('Path to your chrome webdriver')
      wb.get('https://na.op.gg/champion/statistics')  
      

      顶级层

      y_top = {}
      tbody_top = wb.find_elements_by_xpath('//table[@class = "champion-index-table tabItems"]/tbody[@class="tabItem champion-trend-tier-TOP"]/tr')
      for i in range(len(tbody_top)):
          y_top[wb.find_element_by_xpath('//table[@class = "champion-index-table tabItems"]/tbody[@class="tabItem champion-trend-tier-TOP"]/tr['+str(i+1)+']/td[4]/a/div[1]').text] = wb.find_element_by_xpath('//table[@class = "champion-index-table tabItems"]/tbody[@class="tabItem champion-trend-tier-TOP"]/tr['+str(i+1)+']/td[5]').text.rstrip('%')  
      

      适合丛林

      y_jung = {}
      tbody_jung = wb.find_elements_by_xpath('//table[@class = "champion-index-table tabItems"]/tbody[@class="tabItem champion-trend-tier-JUNGLE"]/tr')
      for i in range(len(tbody_jung)):
          y_jung[wb.find_element_by_xpath('//table[@class = "champion-index-table tabItems"]/tbody[@class="tabItem champion-trend-tier-JUNGLE"]/tr['+str(i+1)+']/td[4]/a/div[1]').text] = wb.find_element_by_xpath('//table[@class = "champion-index-table tabItems"]/tbody[@class="tabItem champion-trend-tier-JUNGLE"]/tr['+str(i+1)+']/td[5]').text.rstrip('%')  
      

      中间

      y_mid = {}
      tbody_mid = wb.find_elements_by_xpath('//table[@class = "champion-index-table tabItems"]/tbody[@class="tabItem champion-trend-tier-MID"]/tr')
      for i in range(len(tbody_mid)):
          y_mid[wb.find_element_by_xpath('//table[@class = "champion-index-table tabItems"]/tbody[@class="tabItem champion-trend-tier-MID"]/tr['+str(i+1)+']/td[4]/a/div[1]').text] = wb.find_element_by_xpath('//table[@class = "champion-index-table tabItems"]/tbody[@class="tabItem champion-trend-tier-MID"]/tr['+str(i+1)+']/td[5]').text.rstrip('%')  
      

      底部

      y_bott = {}
      tbody_bott = wb.find_elements_by_xpath('//table[@class = "champion-index-table tabItems"]/tbody[@class="tabItem champion-trend-tier-ADC"]/tr')
      for i in range(len(tbody_bott)):
          y_bott[wb.find_element_by_xpath('//table[@class = "champion-index-table tabItems"]/tbody[@class="tabItem champion-trend-tier-ADC"]/tr['+str(i+1)+']/td[4]/a/div[1]').text] = wb.find_element_by_xpath('//table[@class = "champion-index-table tabItems"]/tbody[@class="tabItem champion-trend-tier-ADC"]/tr['+str(i+1)+']/td[5]').text.rstrip('%')  
      

      支持

      y_sup = {}
          tbody_sup = wb.find_elements_by_xpath('//table[@class = "champion-index-table tabItems"]/tbody[@class="tabItem champion-trend-tier-SUPPORT"]/tr')
      for i in range(len(tbody_sup)):
          y_sup[wb.find_element_by_xpath('//table[@class = "champion-index-table tabItems"]/tbody[@class="tabItem champion-trend-tier-SUPPORT"]/tr['+str(i+1)+']/td[4]/a/div[1]').text] = wb.find_element_by_xpath('//table[@class = "champion-index-table tabItems"]/tbody[@class="tabItem champion-trend-tier-SUPPORT"]/tr['+str(i+1)+']/td[5]').text.rstrip('%')
      
      print(max(y_top,key = y_top.get)) # it will print the max win rate for TOP tier
      print(max(y_jung,key = y_jung.get))
      print(max(y_mid,key = y_mid.get))
      print(max(y_bott,key = y_bott.get))
      print(max(y_sup,key = y_sup.get))
      

      ===========================================
      您可以使用其属性 xpath 定位任何元素,如下所示:
      1. wb.find_element_by_xpath('//div[@id="the_id_of_div"]/a/span')
      2. wb.find_element_by_xpath('//div[@class="class name"]/p/span')
      3. wb.find_element_by_xpath('//div[@title="title of element"]/p/span')
      4. wb.find_element_by_xpath('//div[@style="style x "]/p/span')
      5. wb.find_elements_by_xpath('//*[contains(text(),"the text u wanna find")]') #may be the page has multiple same text that u wanna search...keep in mind6。找到找到的元素的父级 ==>

      that_found_element.find_element_by_xpath('..') # and you can iterate it to the top most parent using loop  
      

      7.查找元素的兄弟 ===>
      找到前面的元素

      wb.find_element_by_xpath('//span[@id ="theidx"]//preceding-sibling::input')  #this tells target a input tag which is preceding sibling of span tag with id as "theidx"
      

      找到下面的元素

      wb.find_element_by_xpath('//span[@id ="theidy"]//following-sibling::input') #this tells target a input tag which is following sibling of span tag with id as "theidy"
      

      【讨论】:

      • 感谢您的评论,虽然它确实有效,但它使用 webdriver 极大地减慢了进程,并使用了对于我的问题来说确实不需要的 for 循环。你的回答确实帮助我解决了我的问题,所以对于这个和你的其他提示,谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-18
      • 1970-01-01
      • 2014-09-06
      相关资源
      最近更新 更多