Where developers come to connect, share, build and be inspired.


Ask wikipedia from command line using Ruby


this could be a complementary script for your rails application, if you have a rails blog and want to give the user ability to search in wikipedia within the same window, or if you want to crawl wikipedia and extract the text only, the following is a simple straightforward solution.

require 'open-uri'
require 'json'

language = 'en'

print 'What do you need to know? : '
article = URI::encode gets.chomp

request_url = "http://#{language}.wikipedia.org/w/api.php?action=parse&page=#{article}&format=json&prop=text&section=0"

open(request_url) do |file|
  content = JSON.parse(file.read())['parse']['text'].first[1]
  content_text = content.gsub(/<\/?[^>]+>/, '')
  content_text = content_text.gsub(/[[:space:]]+/, ' ')
  content_text = content_text.gsub(/&#[0-9]+;/,'')
  content_text = content_text.gsub(/\[[0-9]+\]/,'')
  puts content_text

as this protip got so many views and comments more than any other protip i wrote, i decided to make it as gem you can find it here : https://github.com/blazeeboy/askwiki


Add a comment