# 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:

- result.query: the query string
- result.url: the corresponding page on Wikipedia
- result.body: a tagged list of strings, for example: [(HEADING,"Title"), (PARAGRAPH,"A paragraph of text.")]. The body discerns three tag types: HEADING, PARAGRAPH and LIST
- results.links: a list of related Wikipedia queries (each of these yields a new result when supplied to the search() command)
- results.categories: a list of categories the query is part of.

## 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