如何获得独特的搜索结果?

时间:2016-06-20 07:03:18

标签: python-2.7 web-scraping duckduckgo-api duckduckgo

我正在使用此(https://github.com/thibauts/duckduckgo)模块来搜索duckduckgo搜索结果:

>>> import duckduckgo
>>> for links in duckduckgo.search('Yellow Chris Martin',max_results=20):
...     print links

在输出中我得到搜索结果,似乎有 重复4次相同的链接

输出:

http://www.youtube.com/watch?v=ZTEKsbLl64w
http://www.youtube.com/watch?v=ZTEKsbLl64w
http://www.youtube.com/watch?v=ZTEKsbLl64w
http://www.youtube.com/watch?v=ZTEKsbLl64w
https://en.wikipedia.org/wiki/Yellow_(Coldplay_song)
https://en.wikipedia.org/wiki/Yellow_(Coldplay_song)
https://en.wikipedia.org/wiki/Yellow_(Coldplay_song)
https://en.wikipedia.org/wiki/Yellow_(Coldplay_song)
http://www.youtube.com/watch?v=1MwjX4dG72s
http://www.youtube.com/watch?v=1MwjX4dG72s
http://www.youtube.com/watch?v=1MwjX4dG72s
http://www.youtube.com/watch?v=1MwjX4dG72s

如何修复此问题并获得与使用搜索引擎时相同的结果。

1 个答案:

答案 0 :(得分:1)

您可以使用将duckduckgo对象转换为列表,然后使用set():

count = 10
while( set(list(duckduckgo.search('Yellow Chris Martin',max_results=count)) ) < some_val ):
    count = count + 1

for links in set(list(duckduckgo.search('Yellow Chris Martin',max_results=count)) :
    print links