如何使用Selenium从字符串中提取数字?

我正在Franker Allgemeine Zeitung Archiv上进行WebScraping,我需要计算“Bürokratie”一词在其文章中出现了多少次。 从2018年10月1日到2018年10月31日,该词出现了75次。 我有字符串“ 75 Treffer”。如何使用硒从该字符串中提取数字75?

ief111111 回答:如何使用Selenium从字符串中提取数字?

此代码以任何方式从字符串中提取数字。

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

希望这将帮助您解决问题。

,

使用“”作为拆分位置调用split函数,然后将其转换为int:

yourString = "75 whatever" 
splitString = yourString.split( ' ' )
yourInt = int( splitString[0] )
本文链接:https://www.f2er.com/3079907.html

大家都在问