1

This is a follow up question to my previous problem

driver = webdriver.Chrome(executable_path="C:/Users/Joonas/PycharmProjects/Dictionaries/chromedriver.exe")
driver.get("http://naturalstattrick.com/games.php")
driver.minimize_window()
away_team = driver.find_element_by_xpath("//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[1]/td[2]") #Arizona
home_team = driver.find_element_by_xpath("//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[2]/td[2]") #Vegas
print(away_team.text, home_team.text)

Output:

Arizona Coyotes Vegas Golden Knights

I want to loop the string mentioned above, so that after each loop (Game) /tr[ ] changes. The strings of next game's teams are as follows:

"//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[3]/td[2]" #Chicago
"//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[4]/td[2]" #Washington

I am trying to build a program that scrapes all the games and prints each game separately on their respective rows as I run the program:

Game1 away team Game1 home team
Game2 away team Game2 home team
Game3 away team Game3 home team

Expected Output:

Arizona Coyotes Vegas Golden Knights
Chicago Blackhawks Washington Capitals
etc....

1 Answer 1

1

Try this :

team_string = "//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[1]/td[2]"
all_team_list = [team_string[:65]+str(i+1)+team_string[66:] for i in range(0,15)] # Change 15 to 107/109
team_text = [driver.find_element_by_xpath(i).text for i in all_team_list]
team_text = zip(*[team_text[i::2] for i in range(2)])
print(*[f'Game {i+1} away team : {awt}, home team : {hmt}' for i, (awt, hmt) in enumerate(team_text)], sep='\n')

Output :

Game 1 away team : Arizona Coyotes, home team : Vegas Golden Knights
Game 2 away team : Chicago Blackhawks, home team : Washington Capitals
Game 3 away team : Dallas Stars, home team : St Louis Blues
Game 4 away team : Boston Bruins, home team : New Jersey Devils
Game 5 away team : Calgary Flames, home team : Vancouver Canucks
Game 6 away team : Montreal Canadiens, home team : New Jersey Devils
Game 7 away team : New York Islanders, home team : Philadelphia Flyers
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.