简体   繁体   中英

How to retrieve a sub-string from a string that changes dynamically with respect to multiple delimiters through Selenium in Python

I wonder if its possible to remove part of the scraped string like:

Wujek Drew / Uncle Drew

into

Uncle Drew

Of course, as it is web scraping, the titles will be different every time, so what can I do here to get the result above?


Update

I forgot to add something that need to be removed also. Wujek Drew / Uncle Drew (2018) I Will need to delete the data at the end of the string.

To remove first part of the scraped string separated by / character you can use the following solution:

value = driver.find_element_by_xpath("element_xpath").get_attribute("innerHTML").split("/")[1] 

As per your comment update if you want to extract the sub-string Uncle Drew from the string Wujek Drew / Uncle Drew (2018) you can use the following solution:

import re

value = driver.find_element_by_xpath("element_xpath").get_attribute("innerHTML")
#value='Wujek Drew / Uncle Drew (2018)'
print(re.split('[/()]',value)[1])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM