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

2024-09-28 22:14:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我在法兰克福Allgemeine Zeitung Archiv网站上拉屎,我需要数一数“B”这个词有多少次ü“罗克拉蒂”出现在他们的文章中。 从2018年10月1日到2018年10月31日,这个词出现了75次。 我有串“75特雷弗”。如何使用selenium从字符串中提取数字75


Tags: 字符串网站selenium文章数字archivzeitungallgemeine
2条回答

这段代码以任何方式从字符串中提取数字

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

希望这能帮助你解决你的问题

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

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

相关问题 更多 >