简体   繁体   中英

How can I extract a number from a string using selenium?

I am doing WebScraping on the Frankfurter Allgemeine Zeitung Archiv and I need to count how many times the word 'Bürokratie' appears in their articles. From 1st October 2018 to 31st October 2018 the word appeared 75 times. I have the string "75 Treffer". How can I extract the number 75 from that string using selenium?

This code extract number any way from the string.

# Python3 code to demonstrate 
# getting numbers from string  
# using List comprehension + isdigit() +split() 

# initializing string  
test_string = "There are 2 Cars for 8 persons"

# printing original string  
print("The original string : " + test_string) 

# using List comprehension + isdigit() +split() 
# getting numbers from string  
res = [int(i) for i in test_string.split() if i.isdigit()] 

# print result 
print("The numbers list is : " + str(res)) 

Hope this will help you to address your issue.

Call the split function using " " as the place to split at then cast to an int:

yourString = "75 whatever" 
splitString = yourString.split( ' ' )
yourInt = int( splitString[0] )

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