NodeBox

Create visual output with Python programming code
Home Download Reference Tutorial Library Gallery About

wikipedia.search()

Syntax

search(query)

Description

Searches and parses Wikipedia for query. The returned value has a number of attributes with data on the given query:

Example

wikipedia = ximport("wikipedia")

result = wikipedia.search("food")
print result.body
>>>[('p', "'Food' is any substance normally eaten or drunk by 
>>>        living organisms. The term food also includes liquid 
>>>        drink/drinks. Food is the main source of energy and
>>>        of nutrition for animals, and is usually of animal or 
>>>        plant origin."),
>>> ...

This shows only the tip of the iceberg of the returned body, Wikipedia's articles are long, well-written and useful in many ways.

An example of how to parse the body list is by catching each tag, and formatting the text accordingly. This example puts all headings in font size 16pt, paragraphs in 8pt, and list items start with a dot:

wikipedia = ximport("wikipedia")

result = wikipedia.search("food")

y = 100
for tag, txt in result.body:

  if tag == wikipedia.HEADING: 
    fontsize(16)
    y += 16

  if tag == wikipedia.PARAGRAPH: 
    fontsize(10)

  if tag == wikipedia.LIST:
    fontsize(10)
    txt = "* " + txt

  text(txt, 100, y, 600)
  y += textheight(txt, 600) * 1.1