как мне остановить эту печать (val.text) от печати одной и той же таблицы 8 раз - PullRequest
0 голосов
/ 17 июня 2020

Я просматриваю таблицы в раскрывающемся списке Matchday. когда выполняется print (val.text), он печатает каждую таблицу 8 раз точно так же, как количество строк, а не только один раз, затем он переходит к следующему Matchday и печатает таблицу 8 раз. Я просто хочу, чтобы она была напечатана один раз. может кто-нибудь помочь мне найти, где проблема. Я также хотел бы добавить таблицы, скопированные в файл Excel, любая помощь будет оценена

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select

driver = webdriver.Chrome('C:\Chrome\chromedriver.exe')
driver.get('https://www.betika.com/mobile/#/virtuals/results')

#Selecting the Season dropdown box
Seasons = Select(driver.find_element_by_css_selector('td:nth-of-type(1) > .virtuals__results__header__select'))

#Selecting the Matchday dropdown box
Matchdays = Select(driver.find_element_by_css_selector('td:nth-of-type(2) > .virtuals__results__header__select'))

import time
#selecting all the options the Seasons dropdowns.
Season = len(Seasons.options)
for items in reversed(range(Season)):
    Seasons.select_by_index(items)
    time.sleep(2)
    #selecting all the options the Matchdays dropdowns.
    Matchday = len(Matchdays.options)
    for items in range(Matchday):
            Matchdays.select_by_index(items)
            time.sleep(2)
            rows = len(driver.find_elements_by_xpath('//*[@id="app"]/main/div/div[2]/table[2]/tbody/tr')) #count number of rows in the table
            columns = len(driver.find_elements_by_xpath('//*[@id="app"]/main/div/div[2]/table[2]/tbody/tr[2]/td')) #count number of columns in the table
           # print(rows)
           # print(columns)
            for r in range(3,rows+1):
                for c in range(1,columns+1):
                    value = driver.find_elements_by_xpath('//*[@id="app"]/main/div/div[2]/table["+str(r)+"]/tbody/tr["+str(c)+"]')
                for val in value:
                    print(val.text)```


1 Ответ

0 голосов
/ 18 июня 2020

Похоже, эта строка - ваша проблема. value = driver.find_elements_by_xpath('//*[@id="app"]/main/div/div[2]/table["+str(r)+"]/tbody/tr["+str(c)+"]')

.format() - вот что я предлагаю использовать. Увидеть ниже. Я закомментировал проблемную строку c и добавил свою обновленную строку.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select

driver = webdriver.Chrome('C:\Chrome\chromedriver.exe')
driver.get('https://www.betika.com/mobile/#/virtuals/results')

#Selecting the Season dropdown box
Seasons = Select(driver.find_element_by_css_selector('td:nth-of-type(1) > .virtuals__results__header__select'))

#Selecting the Matchday dropdown box
Matchdays = Select(driver.find_element_by_css_selector('td:nth-of-type(2) > .virtuals__results__header__select'))

import time
#selecting all the options the Seasons dropdowns.
Season = len(Seasons.options)
for items in reversed(range(Season)):
    Seasons.select_by_index(items)
    time.sleep(2)
    #selecting all the options the Matchdays dropdowns.
    Matchday = len(Matchdays.options)
    for items in range(Matchday):
            Matchdays.select_by_index(items)
            time.sleep(2)
            rows = len(driver.find_elements_by_xpath('//*[@id="app"]/main/div/div[2]/table[2]/tbody/tr')) #count number of rows in the table
            columns = len(driver.find_elements_by_xpath('//*[@id="app"]/main/div/div[2]/table[2]/tbody/tr[2]/td')) #count number of columns in the table
           # print(rows)
           # print(columns)
            for r in range(3,rows+1):
                for c in range(1,columns+1):
                    #value = driver.find_elements_by_xpath('//*[@id="app"]/main/div/div[2]/table["+str(r)+"]/tbody/tr["+str(c)+"]')
                    value = driver.find_elements_by_xpath("//*[@id='app']/main/div/div[2]/table['{}']/tbody/tr['{}']".format(r, c))
                for val in value:
                    print(val.text)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...