繁体   English   中英

Python3.7如何从列表中提取数值

[英]Python3.7 how to extract numerical value from list

我正在做一些CTF,并编写了以下脚本:

import requests

page = requests.get("http://ctf.slothparadise.com/about.php").text
p_split = page.split("<p>")
p2_split = p_split[3].split("</p>")

print(p2_split)

我的输出是:

['You are the 135181th visitor to this page.\n      Every thousandth visitor gets a prize.', '\n    </div> <!-- /container -->\n  </body>\n</html>\n']

如何从列表中提取值135181

您可以尝试使用regex ,这非常容易,因为尽管数字以1或2结尾,但它们似乎并没有改变'th'。

import re
import requests

page = requests.get("http://ctf.slothparadise.com/about.php").text
re.findall("\d+(?=th)", page)

输出:

['135335']

为了使它发挥任何价值,请对此进行调整:

my_split = ['You are the 135181th visitor to this page.\n      Every thousandth visitor 
gets a prize.', '\n    </div> <!-- /container -->\n  </body>\n</html>\n']

visitor_num = my_split[0].split('You are the ', 1)[1].split('th')[0]
print(visitor_num)

实际上,我也可以在评论中看到类似的解决方案……希望这对您有用! 为了将来参考,请使用split函数和索引进行查找-您一定会再次使用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM