(searchterm)
return u"{0}\n{1}".format(p, link)
return the top wiki search result for the term
return the top wiki search result for the term
return the top wiki search result for the term
def wiki(searchterm):
    """return the top wiki search result for the term"""
    searchterm = quote(searchterm)

    url = "https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch={0}&format=json"
    url = url.format(searchterm)

    result = requests.get(url).json()

    pages = result["query"]["search"]

    # try to reject disambiguation pages
    pages = [p for p in pages if 'may refer to' not in p["snippet"]]

    if not pages:
        return ""

    page = quote(pages[0]["title"].encode("utf8"))
    link = "http://en.wikipedia.org/wiki/{0}".format(page)

    r = requests.get(
        "http://en.wikipedia.org/w/api.php?format=json&action=parse&page={0}".
        format(page)).json()
    soup = BeautifulSoup(r["parse"]["text"]["*"], "html5lib")
    p = soup.find('p').get_text()
    p = p[:8000]

    return u"{0}\n{1}".format(p, link)
def wiki ( searchterm ) : searchterm = quote ( searchterm ) url = "https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch={0}&format=json" url = url . format ( searchterm ) result = requests . get ( url ) . json ( ) pages = result [ "query" ] [ "search" ] # try to reject disambiguation pages pages = [ p for p in pages if 'may refer to' not in p [ "snippet" ] ] if not pages : return "" page = quote ( pages [ 0 ] [ "title" ] . encode ( "utf8" ) ) link = "http://en.wikipedia.org/wiki/{0}" . format ( page ) r = requests . get ( "http://en.wikipedia.org/w/api.php?format=json&action=parse&page={0}" . format ( page ) ) . json ( ) soup = BeautifulSoup ( r [ "parse" ] [ "text" ] [ "*" ] , "html5lib" ) p = soup . find ( 'p' ) . get_text ( ) p = p [ : 8000 ] return u"{0}\n{1}" . format ( p , link )