Skip to content

Query wiki from command line

by admin on January 12th, 2012
#!/usr/bin/env python

__author__ = "Tom Luo"
__version__ = "$Revision 0.1 $"
__date__ ="$Date: 2012/01/11 11:11PM MST$"

import os, urllib, sys
import re

def f(s):
    return s.find('remove the lines that containt this string') < 0

#url = 'http://en.wikipedia.org/wiki/' + '_'.join(sys.argv[1:])
url = 'http://en.wikipedia.org/w/index.php?search=' + '+'.join(sys.argv[1:])
fobj= os.popen("lynx -dump -width=120 %s" % url)
output = fobj.read()
fobj.close()
start = output.find("From Wikipedia, the free encyclopedia")
end = output.find("Terms of use for details")
lines = output[start:end]
lines = reversed(re.split('\[\d+\]',lines))
lines = filter(f,lines)
print '.'.join(lines)

Save the above script as wiki
Examples

$chmod +x wiki
$wiki china
$wiki barack obama
$wiki austin tx
$wiki salem oregon

From → Linux

No comments yet

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS