首页 > 解决方案 > 从python字符串数组中删除子字符串

问题描述

我有一个字符串数组,我正在抓取,看起来像这样["https://rotogrinders.com/players/charlie-culberson-13456?format=json", "https://rotogrinders.com/players/charlie-culberson-13456?format=json"]

我想https://rotogrinders.com/players/从数组中的每个字符串中删除部分。我该怎么做?

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'))

标签: pythonstring

解决方案


你可以简单地做:

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]

推荐阅读