0

I have an array of strings that I am scraping that looks like this ["https://rotogrinders.com/players/charlie-culberson-13456?format=json", "https://rotogrinders.com/players/charlie-culberson-13456?format=json"]

I want to remove the https://rotogrinders.com/players/ part from each string in the array. How would I do this?

from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.common.exceptions import TimeoutException

browser = webdriver.Chrome("/ProgramData/chocolatey/bin/chromedriver.exe") 

browser.get("https://rotogrinders.com/projected-stats/mlb-hitter?site=fanduel")

# Wait 20 seconds for page to load
timeout = 20
try:
    WebDriverWait(browser, timeout).until(EC.visibility_of_element_located((By.CLASS_NAME, 'player-popup')))
except TimeoutException:
    print("Timed out waiting for page to load")
    browser.quit()

# find_elements_by_xpath returns an array of selenium objects.

players_info = []

players = browser.find_elements_by_css_selector('div.player')

for player in players:
    link = player.find_element_by_class_name('player-popup')
    players_info.append(link.get_attribute('href'))

1 Answer 1

2

You could simply do:

listbase = ["https://rotogrinders.com/players/charlie-culberson-13456?format=json",
        "https://rotogrinders.com/players/charlie-culberson-13456?format=json"]
_list = [element.split("/", maxsplit=4)[-1] for element in listbase]
Sign up to request clarification or add additional context in comments.

2 Comments

Similarly, you could so something like [u.rsplit('/')[-1] for u in urls]. As an aside, it is usually a good idea to avoid using built-ins like list as variable names.
uups - you're right @benvc - it was just quick'n dirty ;)

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.