How Use Re.sub To Convert Selenium String To An Integer
Solution 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 re 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:
Solution 2:
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))
Post a Comment for "How Use Re.sub To Convert Selenium String To An Integer"