如何排除所有标题与查找? [英] how to exclude all title with find?

查看:72
本文介绍了如何排除所有标题与查找?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有从我的网站获取所有标题的功能 我不想从某些产品中获得标题 这是正确的方法吗? 我不希望产品中带有"OLP NL"或"Arcserve"或"LicSAPk"或"symantec"的标题

i have function that get me all the titles from my website i dont want to get the title from some products is this the right way ? i dont want titles from products with the words "OLP NL" or "Arcserve" or "LicSAPk" or "symantec"

def get_title ( u ):
html = requests.get ( u )
bsObj = BeautifulSoup ( html.content, 'xml' )
title = str ( bsObj.title ).replace ( '<title>', '' ).replace ( '</title>', 
'' )
if (title.find ( 'Arcserve' ) or title.find ( 'OLP NL' ) or title.find ( 
'LicSAPk' ) or title.find (
        'Symantec' ) is not -1):
    return 'null'
else:
    return title

            if (title != 'null'):
            ws1 [ 'B1' ] = title
            meta_desc = get_metaDesc ( u )
            ws1 [ 'C1' ] = meta_desc
            meta_keyWrds = get_metaKeyWrds ( u )
            ws1 [ 'D1' ] = meta_keyWrds
            print ( "writing product no." + str ( i ) )
        else:
            print("skipped product no. " + str ( i ))
            continue;

问题在于该程序排除了我所有的产品,而我所看到的只是跳过的产品编号". ? ay?并非所有人都有这些话……

the problem is that the program exclude all my products and all i'm seeing is "skipped product no." ? whay ? not all of them have these words ...

推荐答案

您可以更改(title.find ( 'Arcserve' )!=-1 or title.find ( 'OLP NL' )!=-1 or title.find ('LicSAPk' )!=-1 or title.find ('Symantec' )!=-1)的if语句,也可以创建一个函数来评估要查找的术语

You can change the if statement for (title.find ( 'Arcserve' )!=-1 or title.find ( 'OLP NL' )!=-1 or title.find ('LicSAPk' )!=-1 or title.find ('Symantec' )!=-1) or you can create a function to evaluate the terms that you want to find

def TermFind(Title):
    terms=['Arcserve','OLP NL','LicSAPk','Symantec']
    disc=False
    for val in terms:
        if Title.find(val)!=-1:
            disc=True
            break
    return disc

当我使用if语句时,无论标题值如何,始终返回True.我找不到这种行为的解释,但您可以尝试检查此[ Python!=操作与不是" 和[嵌套和/或" if语句.希望能帮助到你.

When I used the if statement always returned True regardless of the title value. I couldn't find an explanation for such behavior, but you can try checking this [Python != operation vs "is not" and [nested "and/or" if statements. Hope it helps.

这篇关于如何排除所有标题与查找?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆