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

谁说我不能喝 提交于 2021-01-28 08:50:43

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/52121094/how-to-retrieve-a-sub-string-from-a-string-that-changes-dynamically-with-respect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!