python重复单词检查_使用python检查单词是否在网页中出现两次

我登录到一个站点,然后搜索该站点.从结果中,我正在搜索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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值