python - 从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'))
解决方案
你可以简单地做:
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]
推荐阅读
- html - 我应该给 div 一个非常具体的类名,然后用类选择器或非常通用的名称选择它,然后用后代选择器选择它
- haskell - 类型构造函数可以与值构造函数相同吗?
- java - 尝试将应用程序部署到本地 Tomcat 服务器时出错
- java - 使用 Hibernate Provider 的 JUnit 测试 JPA (java.lang.NoSuchMethodError: javax.persistence.spi.PersistenceUnitInfo...)
- python-3.5 - 从两个列表创建自定义组合
- ms-access - 如果从 VBA 代码打开远程 Access 数据库,为什么退出该实例时它会变得可见一秒钟?
- c# - 如何检查是否加载了 .exe 文件来执行它?
- angularjs - 有没有办法在 Angular 中将访问令牌转换为 Base64 编码的字符串?
- c# - 求连续素数之和
- django - 本地设置找不到应用