我有一个url列表(unicode
),有很多重复。
例如,url http://www.myurlnumber1.com
和http://www.myurlnumber1.com/foo+%bar%baz%qux
指向同一个地方。你知道吗
所以我需要剔除所有的重复。你知道吗
我的第一个想法是检查元素的子字符串是否在列表中,如下所示:
for url in list:
if url[:30] not in list:
print(url)
但是,它试图将文本url[:30]
转换为一个列表元素,并且显然返回所有元素,因为没有与url[:30]
完全匹配的元素。你知道吗
有没有简单的方法来解决这个问题?你知道吗
编辑:
通常,URL中的主机和路径保持不变,但参数不同。出于我的目的,一个具有相同主机名和路径,但不同参数的url仍然是相同的url,并构成一个重复的url。你知道吗
您可以尝试添加另一个for循环,如果您不介意的话。 比如:
将每个单词与其他单词进行比较,以检查是否相同。这只是一个例子,我相信你可以让它更强大。你知道吗
如果您认为任何netloc都是相同的,那么可以使用^{} 进行解析
这会给你:
因此,要获得独特的Netloc,您可以执行以下操作:
如果要保留url方案:
假设它们都有方案,而您没有相同netloc的http和https,并认为它们是相同的。你知道吗
如果还要添加路径:
文档中列出了属性表:
你只需要使用你认为独特的部分。你知道吗
相关问题 更多 >
编程相关推荐