I am trying to scrape the sportsbook page. So far I can open a page and go to the live section. Then I try to locate a particular match using By.XPATH to find an element containing the team name is where I get in trouble. Getting error message: 'str' object is not callable. Here is my code:
from lib2to3.pgen2.driver import Driver
from urllib import request
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import pandas as pd
from selenium.webdriver.common.action_chains import ActionChains
from bs4 import BeautifulSoup
import requests
superbet_team1_name = 'Dubrava'
superbet_team2_name = 'Western United'
driver = webdriver.Chrome('C:/Users/tmarkac/source/repos/chromedriver.exe')
page = driver.get('https://superbet.pl/zaklady-bukmacherskie/live')
driver.find_element(By.XPATH,'//*[@id="onetrust-accept-btn-handler"]').click()
time.sleep(3)
#soup = BeautifulSoup(driver.page_source,'lxml')
event = driver.find_element(By.XPATH("//button[@title = 'Dubrava']")) `# Dubrava team name is just an example, any team name can be used for solution.`
For some reason, the last line of code is not working. Also, if there is a way to pass the variable superbet_team1_name
as the value of the title attribute would be great. Appreciate all the help.
CodePudding user response:
change this line
event = driver.find_element(By.XPATH("//button[@title='Dubrava']"))
to this
event = driver.find_element(By.XPATH,"//button[@title='Dubrava']")
to pass as a variable use python format()
function
superbet_team1_name = 'Dubrava'
event = driver.find_element(By.XPATH,"//button[@title='{}']".format(superbet_team1_name ))
CodePudding user response:
- You can make a list of keywords and inject into the url end point
- After driver.page_source driver will not work
- In bs4 to parse element coockie acceptance is not mandatory
Dubrava
contains no result at present
Example:
import time
from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
webdriver_service = Service("./chromedriver") #Your chromedriver path
driver = webdriver.Chrome(service=webdriver_service)
# superbet_team1_name = 'Dubrava'
# superbet_team2_name = 'Western United'
team_names = ['Dubrava','Western United']
for team_name in team_names:
#url ='https://superbet.pl/wyszukaj?query=dubrava'
u =f'https://superbet.pl/wyszukaj?query={team_name}'
driver.get(u)
driver.maximize_window()
time.sleep(10)
soup = BeautifulSoup(driver.page_source,"lxml")
v= soup.select_one('.event-row__layout')
v = v.get_text(' ',strip=True) if v else None
print(v)
Output:
16.10 12:00 Western United Sydney FC 2848 Mecz 1 2.75 2.75 X 3.40 3.40 2 2.60 2.60 96