You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中使用循环与条件过滤URL失效问题求助

问题分析与解决

你的过滤逻辑存在错误,导致所有URL都被保留。原代码中,对每个URL会遍历filterlist的所有元素,只要有一个元素不在URL中,就把该URL添加到结果列表。比如YouTube的URL,虽然包含youtube.com,但它不包含twitter.com等其他4个元素,因此会触发添加操作,最终所有URL都被保留下来。

错误代码问题点

weblinks =[]
fetchurl = ['https://www.youtube.com/watch?v=___nkvwpnAg&t=1218', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://wonderfulwanderings.com/interesting-facts-about-the-netherlands/'] 

filterlist = ["youtube.com","twitter.com","facebook.com","google.com","tiktok.com"]
for ur in fetchurl:
    for i in range(len(filterlist)):
        if filterlist[i] not in ur:
            weblinks.append(ur)  # 只要有一个过滤词不在URL里就添加,逻辑错误
print("weblinks url working", weblinks)

修正方案

我们需要判断:URL不包含filterlist中的任何一个元素时,才将其加入结果列表。以下两种写法都可以实现需求:

方案1:使用any()函数(简洁写法)

weblinks = []
fetchurl = ['https://www.youtube.com/watch?v=___nkvwpnAg&t=1218', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://wonderfulwanderings.com/interesting-facts-about-the-netherlands/'] 

filterlist = ["youtube.com","twitter.com","facebook.com","google.com","tiktok.com"]
for ur in fetchurl:
    # 如果URL中不包含filterlist里的任何一个元素,才添加
    if not any(domain in ur for domain in filterlist):
        weblinks.append(ur)
print("weblinks url working", weblinks)

方案2:调整循环逻辑(直观写法)

weblinks = []
fetchurl = ['https://www.youtube.com/watch?v=___nkvwpnAg&t=1218', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://wonderfulwanderings.com/interesting-facts-about-the-netherlands/'] 

filterlist = ["youtube.com","twitter.com","facebook.com","google.com","tiktok.com"]
for ur in fetchurl:
    should_keep = True
    for domain in filterlist:
        if domain in ur:
            should_keep = False
            break  # 一旦发现包含过滤域名,直接跳过后续检查
    if should_keep:
        weblinks.append(ur)
print("weblinks url working", weblinks)

修正后输出

运行上述代码后,输出会过滤掉YouTube的URL,只保留符合要求的6个URL:

weblinks url working ['https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://wonderfulwanderings.com/interesting-facts-about-the-netherlands/']

内容的提问来源于stack exchange,提问作者Info Rewind

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 15:20:32