关于 URL 去重的想法

2015-03-01 16:23:02 +08:00
 chuhades

最近遇到了这个问题。目前的方案是,提取 url 的 请求方法|协议|域名|一级目录|目录深度|尾部特征|QUERY|DATA 作为特征,md5(特征) 作为返回值。
可以判断如下这种:
http://www.yigeshop.cn/index.php/home/shop_list/9
http://www.yigeshop.cn/index.php/home/shop_list/16
但对于 http://www.yigeshop.cn/index.php/home/shop_details/24,算法认为它与上面两个相同。
各位有什么好的解决方案么?

贴上渣渣算法:
```
def hash(self):
"""
URL 去重
:return: hash
:rtype: int
"""
# 请求方法|协议|域名|一级目录|目录深度|尾部特征|QUERY|DATA
# http://a.com/p1/p2/p3/f.php?a=1&b=2&c
# GET|http|a.com|p1|4|php|abc|

first_dir = self.__parsed.path.split("/")[1] \
        if len(self.__parsed.path.split("/")) > 1 else ""
    depth = str(self.__parsed.path.count("/")) \
        if self.__parsed.path.count("/") else "1"
    suffix = self.__parsed.path.split("/")[-1].split(".")[-1] \
        if "." in self.__parsed.path.split("/")[-1] else ""
    query = "".join(sorted(self.query.keys()))
    data = "".join(sorted(self.__data.keys()))

    feather = "|".join((
        self.__method,
        self.__parsed.scheme,
        self.__parsed.netloc,
        first_dir,
        depth,
        suffix,
        query,
        data
    ))

    hash_ = int(md5(feather.encode()).hexdigest(), 16)

    return hash_
5315 次点击
所在节点    Python
21 条回复
chuhades
2015-03-02 19:33:46 +08:00
@akira 是的,如果已知url量足够大的话,完全可以分析出哪里是参数。就个人的需求而言,做这个去重就是为了减少爬行的url数目。。所以感觉是个死结 : (
依旧感谢。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/173756

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX