python - 如何用查找排除所有标题?
问题描述
我有从我的网站获取所有标题的功能我不想从某些产品中获取标题这是正确的方法吗?我不想要带有“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;
问题是该程序排除了我所有的产品,而我看到的只是“跳过的产品编号”。? 为什么?不是所有人都有这些话……
解决方案
您可以更改 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。希望能帮助到你。
推荐阅读
- python - 如何更改像素坐标?
- python - 按一列中的一个元素列表的值过滤熊猫数据框
- amazon-web-services - 跨账户 Lambda 到 API 网关设置
- javascript - React Router - 处理来自服务器和渲染组件的 403 响应
- vue.js - 在 render() 中以编程方式访问 Vue 过滤器
- mysql - 运行 MYSQL 生成数据库
- java - 在循环中将 1 添加到 Integer.MAX_VALUE 会导致意外行为(而不是它转到 Integer.MIN_VALUE 的行为)
- c - 如果我 printf 一个没有匹配参数的整数,具体确定输出的是什么?
- exception - React 和 sequelize 错误处理以显示来自前端的消息
- javascript - Openlayers 3 地图,如何在没有焦点的情况下刷新