.XPath для предметов под определенным тд - PullRequest
0 голосов
/ 17 октября 2019

Я пытаюсь почистить веб-сайт (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> 

Затем следующий переходит к

<tbody class="tabItem champion-trend-tier-JUNGLE" style="display: table-row-group;">

Итак, я попробовал это, но он не выводит ничего, кроме []. Надеюсь, мой вопрос имеет смысл.

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

Ответы [ 2 ]

0 голосов
/ 19 октября 2019

Я смог достичь своей цели, просто выполнив

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

Спасибо за чтение!

0 голосов
/ 18 октября 2019

Вы можете пройти через всю строку кода, я уверен, что вы получите ответ. это позволяет легко и надежно настроить таргетинг на элемент в xpath

Начальная настройка

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

Для уровня TOP

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 mind
6. найти родителя найденного элемента ==>

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"
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...