最近想提取出特定的URL,遇到问题为预期提取出URL中带有webshell或者phpinfo字段的URL,但是全部URL都匹配出来了:
for url in urls:
    if "webshell" or "phpinfo" in url:
        print url

改成and语句也不符合预期,只提取出了含有phpinfo的url:
for url in urls:
    if "webshell" and "phpinfo" in url:
        print url

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
这样才可以, 你原来的是先判断"webshell",如果不为零再判断"phpinfo" in url. "webshell" 和 "phpinfo" in url并列...
這樣做的意思是
if "webshell"orif "phpinfo" in url而前者恆成立。這樣做的意思是
if "phpinfo" in url因為if "webshell"恆成立。解法基本上如 @洛克 所說:
如果今天用來匹配的 word 很多的話:
結果:
urlcontain(url, lst)可以問url裡面是不是有lst裡面的任何一個 string這樣子要比對十個關鍵字也不會寫出太長的 if 述句。
當然要用
re也可以,只是我個人不太喜歡re就是了...我回答過的問題: Python-QA