-1

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?

7
  • Why dont you just split the string by space and get the last element? Commented May 9, 2016 at 12:45
  • @Abbas yeah i can use that but what if the website will change their span text in future. Then it will not work Commented May 9, 2016 at 12:48
  • you can catch the numeric value after Rs with regex and replace the whole string with that Commented May 9, 2016 at 12:51
  • @dnit13 okay. Can you tell me how to do that? Commented May 9, 2016 at 12:53
  • So, do you want to find the last number or the last word? Commented May 9, 2016 at 13:31

1 Answer 1

0

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.