Замена части строки в цикле for на количество выполненных циклов - PullRequest
0 голосов
/ 21 сентября 2019

Как я могу изменить определенную часть строки с количеством циклов, которые я сделал?

Я собираю данные с веб-сайта, используя Chromedriver (find_element_by_xpath).Я вижу, что проблема возникает, когда число циклов превышает 10. Пока count не достигнет 10, он смог заменить / tr [1] на / tr [2] и так далее, но как только появятся двойные цифрыпо ходу дела усложняются.

count = 0
for i in range(0, 21):
    count += 1
    string[65] = str(count)
    string = ''.join(string)
    print(string)
    string = list(string)

The output is:
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[1]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[2]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[3]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[4]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[5]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[6]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[7]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[8]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[9]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[10]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[110]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[1210]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[13210]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[143210]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[1543210]/td[2]


And what I expect output to be is:
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[1]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[2]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[3]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[4]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[5]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[6]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[7]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[8]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[9]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[10]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[11]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[12]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[13]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[14]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[15]/td[2]

1 Ответ

0 голосов
/ 21 сентября 2019

Попробуйте:

string = "//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[1]/td[2]"
all_list = [string[:65]+str(i+1)+string[66:] for i in range(0,15)]
print(*all_list, sep='\n')

Вывод :

//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[1]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[2]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[3]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[4]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[5]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[6]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[7]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[8]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[9]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[10]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[11]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[12]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[13]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[14]/td[2]
//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[15]/td[2]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...