Search code examples
python-2.7web-scrapingduckduckgo-apiduckduckgo

How to get unique search results?


I am using this(https://github.com/thibauts/duckduckgo) module to scrape duckduckgo search results:

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

In the output I am getting search results and there seems to be
repetition of 4 times of the same link

Output:

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

How to fix this and get same results as found when using the search engine.


Solution

  • You could use convert the duckduckgo object to a list and then use 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