website = str(raw_input('Website: '))
palavras_chaves = ['joomla', 'Joomla']
palavras_chaves2 = ['wordpress', 'Wordpress', 'WordPress', 'wp']
abrindo = urllib2.urlopen(website)
abrindo = abrindo.read()
if palavras_chaves[0] and palavras_chaves[1] in abrindo:
print '\nÉ um joomla!'
elif palavras_chaves2[0] and palavras_chaves2[1] and palavras_chaves2[2] and palavras_chaves2[3] in abrindo:
print '\nÉ um wordpress!'
else:
print '\nÉ um CMS desconhecido!'
我想知道如何检查所有字符串而不指定它。似乎很简单,但我尝试了一些我想到的并且没有工作的东西。
答案 0 :(得分:2)
正如@fourtheye指出的那样,你需要内置的all()
函数,你可以利用列表理解来完成这项工作,如下所示。
所以试试这个:
if all(i in abrindo for i in palavras_chaves):
print '\nÉ um joomla!'
elif all(i in abrindo for i in palavras_chaves2):
print '\nÉ um wordpress!'
else:
print '\nÉ um CMS desconhecido!'
我认为如果您手动执行此操作:
if palavras_chaves[0] and palavras_chaves[1] in abrindo:
可行,因为palavras_chaves[1]
位于abrindo
但palavras_chaves[0]
可能不在abrindo
,但在all()
使用palavras_chaves
abrindo
时 位于abrindo
,以防一个字符串不在'Works as expected'
, 它失败! < / p>
请参阅此示例,您尝试执行此类操作,并且您希望它返回>>> if 'Hey' and 'Bye' in 'Bye':
... print 'Not expected right?'
... else:
... print 'Works as expected'
...
#Not expected right?
但是它会返回相反的
all()
但是使用if all(i in 'Bye'for i in ['Hey','Bye']):
print 'Not expected right?'
else:
print 'Works as expected'
#Works as expected
:
{{1}}
答案 1 :(得分:-1)
使用python的内置集,它具有issubset方法。 http://docs.python.org/2/library/stdtypes.html#set