Search DuckDuckGo from Command-line

Simple python script using robobrowser package to search duckduckgo.com from command-line.


~/bin/duckduckgo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/python

from robobrowser import RoboBrowser
from sys import argv

def results(q):
browser = RoboBrowser()
browser.open('https://duckduckgo.com/')

form = browser.get_form(id='search_form_homepage')
form['q'].value = q

browser.submit_form(form)

return [(result['href'], result.text) for result in browser.select('.result__a')]

if __name__ == "__main__":
CLEAR='\033[0m'
BOLD='\033[1m'
BLUE='\033[94m'
if len(argv) > 1 and argv[1]:
for result in results(' '.join(argv[1:])):
print BOLD, result[1], CLEAR, ' - ', BLUE, result[0], CLEAR

duckduckgo python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Welcome to Python.org   -   https://www.python.org/
Learn Python - Free Interactive Python Tutorial - http://www.learnpython.org/
Python (programming language) - Wikipedia, the free encyclopedia - https://en.wikipedia.org/wiki/Python_%28programming_language%29
Python - Wikipedia, the free encyclopedia - https://en.wikipedia.org/wiki/Python
Python - Download.com - http://download.cnet.com/Python/3000-2069_4-10080057.html
Overview — Python 3.5.2rc1 documentation - https://docs.python.org/
BeginnersGuide - Python Wiki - https://wiki.python.org/moin/BeginnersGuide
Download Python | Python.org - https://www.python.org/downloads/
PyPI - the Python Package Index - https://pypi.python.org/
The Python Tutorial — Python 2.7.12rc1 documentation - https://docs.python.org/2/tutorial/
Python | Codecademy - https://www.codecademy.com/learn/python
Welcome to Python.com || $40 per signup! - http://www.python.com/inside.phtml?source=home&
PythonGB - YouTube - http://www.youtube.com/user/PythonGB
Python2orPython3 - Python Wiki - https://wiki.python.org/moin/Python2orPython3
Learn Python The Hard Way - http://learnpythonthehardway.org/book/
...

The links should be clickable in terminal, optionally, create an alias ddg=duckduckgo 2>/dev/null.

DuckDuckGo!