user14915996
user14915996

Reputation:

How use re.sub to convert selenium string to an Integer

I am really desperate his last days to convert my string numbers value to a integer, so i try this :

Snapshot:

enter image description here

Code trials:

follower_count = int(browser.find_element_by_xpath('/html/body/div[1]/section/main/div/header/section/ul/li[2]/a/span').text)
convert_follower_count = re.sub('[^0-9]','', follower_count)

... is not working :

Traceback (most recent call last):
 File "C:\Users\Desktop\TwitPy\quickstart.py", line 79, in <module>
   followers_count = re.sub('[^0-9]','', follower_count)
 File "C:\Users\AppData\Local\Programs\Python\Python38\lib\re.py", line 208, in sub
   return _compile(pattern, flags).sub(repl, string, count)
TypeError: expected string or bytes-like object          

Upvotes: 1

Views: 361

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193298

The extracted text i.e. 1,961 contains a , character in between. So you won't be able to invoke int() directly on it.


Solution

You can can use sub() from to replace the , character from the text 1,961 first and then invoke int() as follows:

  • Code Block:

    # follower_count = int(browser.find_element_by_xpath('/html/body/div[1]/section/main/div/header/section/ul/li[2]/a/span').text)
    count = "1,961"
    
    import re
    print(int(re.sub(',', '', count)))
    print(type(int(re.sub(',', '', count))))
    
  • Console Output:

    1961
    <class 'int'>
    

This usecase

Effectively, your line of code will be:

follower_count = int(re.sub(',', '', browser.find_element_by_xpath('/html/body/div[1]/section/main/div/header/section/ul/li[2]/a/span').text))

References

You can find a relevant detailed discussion in:

Upvotes: 1

CyDevos
CyDevos

Reputation: 411

The TypeError here is because you have converted follower_count into an integer and then passed it to re.sub that accept only string or bytes-like object. You should check the output of browser.find_element_by_xpat and see if it's a string. If it is, you should not encounter any error. If it's a list or an object from selenium you might have to do a little bit more than just pass follower_count to re.sub

follower_count = browser.find_element_by_xpath('/html/body/div[1]/section/main/div/header/section/ul/li[2]/a/span').text
convert_follower_count = int(re.sub('[^0-9]','', follower_count))

Upvotes: 0

Related Questions