我登录到一个站点,然后搜索该站点.从结果中,我正在搜索html,看是否找到匹配项.除了网站显示“ xyz的搜索结果”这一事实外,其他所有功能都可以正常运行,因此我总是在得到负数时得到正数.我当前的代码
... Previous code to log in etc...
words = ['xyz']
br.open ('http://www.example.com/browse.php?psec=2&search=%s' % words)
html = br.response().read()
for word in words:
if word in html:
print "%s found." % word
else:
print "%s not found." % word
作为解决方案,我想检查单词是否出现两次或更多次,如果出现,则为肯定.如果仅出现一次,则很明显只是在搜索“ xyz的搜索结果”,所以找不到它.我将如何修改当前代码以检查两次而不是一次?
谢谢
解决方法:
你可以试试看
for word in words:
if html.count(word)>1:
#your logic goes here
例
>>> words =['the.cat.and.hat']
>>> html = 'the.cat.and.hat'
>>> for w in words:
... if html.count(w)>1:
... print 'more than one match'
... elif html.count(w) == 1:
... print 'only one match found'
... else:
... print 'no match found'
...
only one match found
>>>
标签:python