0

I would like to extract all the url links of soocer events in this webpage : https://www.coteur.com/cotes-foot.php

When I try one match by one using xpath it is ok, you can see it in my code below for fixture1 and fixture2. However I want to extract automatically all the soccer events so that I used a loop but it do not work. It seems that it is not possible to use a loop inside xpath fonction. How to solve this problem ?

#!/usr/bin/python3
# -*- coding: utf­-8 ­-*-

from selenium import webdriver
from bs4 import BeautifulSoup

driver = webdriver.Firefox()
url = 'https://www.coteur.com/cotes-foot.php'
driver.get(url)

fixture1 = driver.find_element_by_xpath("/html/body/div[3]/div/div[2]/div/div/div[2]/div/table/tbody/tr[3]/td[3]/a")
print(fixture1.text)

fixture2 = driver.find_element_by_xpath("/html/body/div[3]/div/div[2]/div/div/div[2]/div/table/tbody/tr[23]/td[3]/a")
print(fixture2.text, '\n')

links = []
i = 3
while i <= 23:
    fixture = driver.find_element_by_xpath("/html/body/div[3]/div/div[2]/div/div/div[2]/div/table/tbody/tr[i]/td[3]/a")
    links.append(fixture)
    i = i + 1

print(links)

driver.close()
ahmedaao
  • 377
  • 1
  • 3
  • 14
  • Does this answer your question? [Which is the preferred way to concatenate a string in Python?](https://stackoverflow.com/questions/12169839/which-is-the-preferred-way-to-concatenate-a-string-in-python) – andrewJames Jun 15 '20 at 14:04

2 Answers2

0

when you put 'i' inside quotes, it is interpreted as a character - not as a variable, so i suggest you try this:

while i <= 23:
    fixture = driver.find_element_by_xpath("/html/body/div[3]/div/div[2]/div/div/div[2]/div/table/tbody/tr[" + str(i) + "]/td[3]/a")
    links.append(fixture)
    i = i + 1
ozymandias
  • 11
  • 1
  • 3
0

I get this :

hao@hao-ThinkPad-T420:~$ ./coteur2.py 
Maccabi Netanya - Bnei Yehuda Tel Aviv
Maritimo - Gil Vicente 

[<selenium.webdriver.firefox.webelement.FirefoxWebElement (session="2cb804be-ea18-4a5d-9fb9-75b306316126", element="c83b08b1-22ab-4454-95c2-cc6d133b926d")>, <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="2cb804be-ea18-4a5d-9fb9-75b306316126", element="ffef9a50-56aa-480a-b144-59a1bb12e5c9")>]

I am looking for the name of the event like fixture1 and fixture2

ahmedaao
  • 377
  • 1
  • 3
  • 14