如何使用查找排除所有标题?

我的功能可以让我从我的网站获取所有标题我不想从某些产品中获取标题这是正确的方法吗?我不想要带有“OLP NL”或“Arcserve”或“LicSAPk”或“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;

问题是该程序排除了我的所有产品,而我看到的只是“跳过的产品编号”。? 为什么?不是所有人都有这些话……


海绵宝宝撒
浏览 177回答 2
2回答

慕桂英4014372

您可以更改 if 语句,(title.find ( 'Arcserve' )!=-1 or title.find ( 'OLP NL' )!=-1 or title.find ('LicSAPk' )!=-1 or title.find ('Symantec' )!=-1)也可以创建一个函数来评估要查找的术语def TermFind(Title):&nbsp; &nbsp; terms=['Arcserve','OLP NL','LicSAPk','Symantec']&nbsp; &nbsp; disc=False&nbsp; &nbsp; for val in terms:&nbsp; &nbsp; &nbsp; &nbsp; if Title.find(val)!=-1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; disc=True&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; return disc当我使用 if 语句时,无论标题值如何,总是返回 True。我找不到这种行为的解释,但是您可以尝试检查此 [ Python != operation vs "is not" 和 [ nested "and/or" if statements。希望能帮助到你。

ibeautiful

类似的想法使用 anyimport requests&nbsp;from bs4 import BeautifulSoupurl = 'https://www.cdsoft.co.il/index.php?id_product=300610&controller=product'html = requests.get(url)bsObj = BeautifulSoup(html.content, 'lxml')title = str ( bsObj.title ).replace ( '<title>', '' ).replace ( '</title>', '' )items = ['Arcserve','OLP NL','LicSAPk','Symantec']if not any(item in title for item in items):&nbsp; &nbsp; print(title)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python