亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用查找排除所有標題?

如何使用查找排除所有標題?

海綿寶寶撒 2021-11-30 16:48:14
我的功能可以讓我從我的網站獲取所有標題我不想從某些產品中獲取標題這是正確的方法嗎?我不想要帶有“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;問題是該程序排除了我的所有產品,而我看到的只是“跳過的產品編號”。? 為什么?不是所有人都有這些話……
查看完整描述

2 回答

?
慕桂英4014372

TA貢獻1871條經驗 獲得超13個贊

您可以更改 if 語句,(title.find ( 'Arcserve' )!=-1 or title.find ( 'OLP NL' )!=-1 or title.find ('LicSAPk' )!=-1 or title.find ('Symantec' )!=-1)也可以創建一個函數來評估要查找的術語


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 != operation vs "is not" 和 [ nested "and/or" if statements。希望能幫助到你。


查看完整回答
反對 回復 2021-11-30
?
ibeautiful

TA貢獻1993條經驗 獲得超6個贊

類似的想法使用 any


import requests 

from bs4 import BeautifulSoup


url = '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):

    print(title)


查看完整回答
反對 回復 2021-11-30
  • 2 回答
  • 0 關注
  • 200 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號