简体   繁体   中英

get last element from a sentence in span using selenium python

I want to scrape last word of a sentence defined in a span using selenium python. I am able to get the whole sentence using this:-

elements = browser.find_element_by_css_selector("span[ng-if='!product.product.isOnlyCarCategory']");
print elements
result = elements.text
print result

Output:- Buy for Rs 329

I want only 329 to be output not the whole sentence. I've tried using replace function of python and able to achieve what I want like this:-

elements = browser.find_element_by_css_selector("span[ng-if='!product.product.isOnlyCarCategory']");
print elements
result = elements.text
result = int(result.replace('Buy for Rs ',''))
print result

Output:- 329

but I don't want to use replace function. I want to use selenium to do the same. And yes this integer value is dynamic

Is there any way to do that?

You can use a regex, or a for loop instead:

strNum = ""
for c in result[::-1]:
    if c.isdigit():
        strNum=c+strNum
    elif len(strNum)>0:
        break
num=into(strNum)
print num

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