1

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          
1
  • Your follower_count is an int. You can't replace characters in it, because it doesn't contain characters. It's a number. Commented Jan 31, 2021 at 20:39

2 Answers 2

1

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:

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

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