Python wikipedia 模块,page() 实例源码

我们从Python开源项目中,提取了以下46个代码示例,用于说明如何使用wikipedia.page()

项目:WikiSummary    作者:Mikerah    | 项目源码 | 文件源码
def get_random_articles_v1(number_of_articles_wanted):
    """Given the wanted number of articles returned, get random wikipedia articles"""
    if number_of_articles_wanted == 1:
        print(wikipedia.summary(wikipedia.random()))
    else:    
        list_of_articles = wikipedia.random(number_of_articles_wanted)
        try:
            for a in list_of_articles:
                article = a[:]
                if ('disambiguation' in wikipedia.page(a).title) or ('it may refer to' in wikipedia.page(a).title):
                    list_of_articles.remove(a)
                    list_of_articles.append(wikipedia.random())

                print(list_of_articles.index(a)+1," - "+wikipedia.summary(a))
                print()
        except wikipedia.exceptions.DisambiguationError:
            list_of_articles.remove(article)
            list_of_articles.append(wikipedia.random(article))
项目:jarvis    作者:anqxyr    | 项目源码 | 文件源码
def tvtropes(inp, *, query):
    """Show laconic description of the trope, and a link to the full page."""
    query = query.title().replace(' ', '')
    baseurl = 'http://tvtropes.org/{}/' + query
    url = baseurl.format('Laconic')
    soup = bs4.BeautifulSoup(requests.get(url).text, 'lxml')
    text = soup.find(class_='page-content').find('hr')
    if text is None:
        return lex.tvtropes.not_found
    text = reversed(list(text.previous_siblings))
    text = [i.text if hasattr(i, 'text') else i for i in text]
    text = [str(i).strip() for i in text]
    return '{} {}'.format(' '.join(text), baseurl.format('Main'))

###############################################################################
# Kaktuskast
###############################################################################
项目:Abb1t    作者:k-freeman    | 项目源码 | 文件源码
def run(self):
        while True:
            msg = self.queue_in.get()  # get() is blocking
            match = re.search(r'^(?:/|!)wiki (.*)$', msg.get_text().lower())
            if match:
                reply = ""
                try:
                    related_entries = wikipedia.search(match.group(1))
                    w = wikipedia.page(match.group(1))
                    reply1 = "*{}*\n".format(w.title)
                    reply2 = "{}\n".format(w.summary)
                    reply3 = "\n*related topics*:\n- {}".format("\n- ".join(related_entries))

                    if len(reply1+reply2+reply3)>4096:
                        reply = reply1 + reply2[:4092-len(reply1)-len(reply3)]+"...\n" + reply3 # shortening to 4096 characters
                    else:
                        reply = reply1+reply2+reply3
                except wikipedia.DisambiguationError as e:
                    related_entries = str(e).split(":",1)[1].split("\n")[1:]
                    reply = "This was too inspecific. Choose one from these:\n- {}".format("\n- ".join(related_entries))
                except:
                    reply = "No matches returned for this request."
                if reply:
                    self.bot.sendMessage(msg.get_chat_id(), reply, parse_mode="Markdown")
项目:AceBot    作者:Run1e    | 项目源码 | 文件源码
def wikirandom(self, ctx):
        """Get a random wikipedia page."""

        await ctx.trigger_typing()
        try:
            page_name = wikipedia.random(1)
        except:
            return await ctx.invoke(self.wikirandom)

        try:
            wiki = wikipedia.page(page_name)
            for attr in ('summary', 'url', 'title'):
                if not hasattr(wiki, attr):
                    return await ctx.invoke(self.wikirandom)
        except wikipedia.exceptions.DisambiguationError as e:
            return await ctx.invoke(self.wikirandom)
        await self.embedwiki(ctx, wiki)
项目:chalice-linebot    作者:c-bata    | 项目源码 | 文件源码
def wikipedia_search(word):
    """Search a word meaning on wikipedia."""
    wikipedia.set_lang('ja')
    results = wikipedia.search(word)

    # get first result
    if results:
        page = wikipedia.page(results[0])
        msg = page.title + "\n" + page.url
    else:
        msg = '`{}` ??????????????'.format(word)
    return msg


# ====================================
# Google News
# ====================================
项目:hangoutsbot    作者:das7pad    | 项目源码 | 文件源码
def wiki(bot, event, *args):
    """lookup a term on Wikipedia"""

    term = " ".join(args)
    if not term:
        return

    try:
        page = wikipedia.page(term, auto_suggest=False)

        summary = page.summary.strip()
        summary = summary.replace('\r\n', '\n').replace('\r', '\n')
        summary = re.sub('\n+', "\n", summary).replace('\n', '\n\n')
        source = _('<i>source: <a href="{}">{}</a></i>').format(page.url, page.url)

        html_text = '<b>"{}"</b>\n\n{}\n\n{}'.format(term, summary, source)
    except wikipedia.exceptions.PageError:
        html_text = _("<i>no entry found for {}</i>").format(term)
    except wikipedia.exceptions.DisambiguationError as e:
        exception_text = str(e).strip().replace("\n", "\n")
        html_text = "<i>{}</i>".format(exception_text)

    return html_text
项目:apex-sigma-core    作者:lu-ci    | 项目源码 | 文件源码
def wikipedia(cmd, message, args):
    if args:
        try:
            summary_task = functools.partial(wiki.page, ' '.join(args).lower())
            with ThreadPoolExecutor() as threads:
                page = await cmd.bot.loop.run_in_executor(threads, summary_task)

            response = discord.Embed(color=0xF9F9F9)
            response.set_author(
                name=f'Wikipedia: {page.title}',
                url=page.url,
                icon_url='https://upload.wikimedia.org/wikipedia/commons/6/6e/Wikipedia_logo_silver.png'
            )
            response.description = f'{page.summary[:800]}...'
        except wiki.PageError:
            response = discord.Embed(color=0x696969, title='?? No results.')
        except wiki.DisambiguationError:
            response = discord.Embed(color=0xBE1931, title='? Search too broad, please be more specific.')
    else:
        response = discord.Embed(color=0xBE1931, title='? Nothing inputted.')
    await message.channel.send(None, embed=response)
项目:tuxbot-bot    作者:outout14    | 项目源码 | 文件源码
def search_aur(self, ctx, args):
        attends = await ctx.send("_Je te cherche ça {} !_".format(ctx.message.author.mention))
        erreur = 0
        try:
            html = urllib.request.urlopen("https://aur.archlinux.org/packages/" + args).read()
        except:
            erreur = 1

        if erreur == 1:
            await attends.delete()
            embed = discord.Embed(description=":sob: Je n'ai pas trouvé le packet mais j'ai lancé une petite recherche, tu y trouveras peut être ton bonheur ? https://aur.archlinux.org/packages/?K=" + args,url='https://aur.archlinux.org/')
            embed.set_author(name="Aur.archlinux", url='https://aur.archlinux.org/', icon_url='http://outout.tech/tuxbot/arch.png')
            embed.set_thumbnail(url='http://outout.tech/tuxbot/arch.png')
            embed.set_footer(text="Pff même pas trouvé !")
            await ctx.send(embed=embed)

        else:
            await attends.delete()
            embed = discord.Embed(description="Et voila, j'ai trouvé la page sur le packet : https://aur.archlinux.org/packages/{0} ! \n Ca te dit un petit ``pacaur -S {0}`` ?".format(args), url='https://aur.archlinux.org/')
            embed.set_author(name="Aur.archlinux", url='https://aur.archlinux.org/', icon_url='http://outout.tech/tuxbot/arch.png')
            embed.set_thumbnail(url='http://outout.tech/tuxbot/arch.png')
            embed.set_footer(text="C'est vrai que pacman et pacaur sont mieux qu'APT ^^")
            await ctx.send(embed=embed)
项目:Mash-Cogs    作者:Canule    | 项目源码 | 文件源码
def wikipedia(self, ctx, *text):
        """Wikipedia search."""     

        if text == ():
            await send_cmd_help(ctx)
            return
        else:            
            s = "_";
            search = ""
            search = s.join(text)
            user = ctx.message.author
            wikiLang = 'en'# Define the Wikipedia language / Most of these are supported » https://nl.wikipedia.org/wiki/ISO_3166-1
            ws = None
            wikipedia.set_lang(wikiLang)# Set the Wikipedia language.
            try:
                ws = wikipedia.page(search)
                wikiUrl = (ws.url.encode('ascii', 'xmlcharrefreplace'))
                await self.bot.say(wikiUrl.decode("utf8"))
            except:
                await self.bot.say( 'Sorry {}, no wiki hit, try to rephrase'.format(user))
项目:cdata    作者:cnschema    | 项目源码 | 文件源码
def wikipedia_search_slow(query, lang="en", max_result=1):
    import wikipedia
    #wikification
    query = any2unicode(query)
    items = []
    ret = {"query":query, "itemList":items}
    wikipedia.set_lang(lang)
    wikiterm = wikipedia.search(query)
    #logging.info(wikiterm)
    for idx, term in enumerate(wikiterm[0:max_result]):
        wikipage = wikipedia.page(term)
        item = {
            "name": wikipage.title,
            "description": wikipedia.summary(term, sentences=1),
            "url": wikipage.url,
        }
        items.append(item)

    return ret
项目:wikipedia_parser    作者:ojones    | 项目源码 | 文件源码
def fetch_wobj(id):
    # TODO: isdigit is not robust enough, a title could be number instead of an id
    wobj = None

    try:
        if str(id).isdigit():
            wobj = wikipedia.page(pageid=id, auto_suggest=False)
        else:
            wobj = wikipedia.page(title=id, auto_suggest=False)
    except:
        # error in 3rd party python-wikipedia package
        pass

    return wobj


# wobj
项目:wikipedia_parser    作者:ojones    | 项目源码 | 文件源码
def fetch_api_categories(id, wobj):

    categories = []

    try:
        if id.isdigit() and wobj:
            categories = wobj.categories
        else:
            page = fetch_mwclient(id)
            for category in list(page.categories()):
                categories.append(category.name)
            return categories
    except:
        pass

    return categories
项目:eatradish_haoyebot    作者:eatradish    | 项目源码 | 文件源码
def wikipedia_summary(msg, lang = 'en'):
    try:
        if lang == 'en':
            wikipedia.set_lang('en')
        else:
            wikipedia.set_lang(lang)
        url = wikipedia.page(msg).url
        msg = wikipedia.summary(msg)
        fliter = []
        for i in msg:
            if i != '\n':
                fliter.append(i)
            else:
                break
        msg = "".join(fliter)
        return msg + '\n' + url
    except:
        return "Not Found Page or LANG"
项目:minbo    作者:minka-it    | 项目源码 | 文件源码
def wiki(message):
    chat_id = message.chat.id
    param = message.text.split(' ',1) #separa el comando de los parametros
    if len(param) == 1 or param[1]=="help":
        bot.send_message(chat_id,text_messages['help_wiki'])
    else:
        bot.send_message(chat_id, "Consultando en Wikipedia...")
        try:
            wiki = wikipedia.page(param[1])
            bot.send_message(chat_id, wiki.summary)
            bot.send_message(chat_id, "Consulta mas en:\n"+wiki.url)
        except wikipedia.exceptions.DisambiguationError as e:
            bot.send_message(chat_id, "'"+param[1]+"'"+" puede referirse a:")
            bot.send_message(chat_id, '\n'.join(e.options))
        except wikipedia.exceptions.PageError as e:
            bot.send_message(chat_id, "No se encontro ninguna pagina, intenta con otra consulta!")
        except Exception, e:
            print e
            bot.send_message(chat_id,"Tengo un bug en mi estomago!")
项目:jarvis    作者:anqxyr    | 项目源码 | 文件源码
def wikipedia(inp, *, query):
    """Get wikipedia page about the topic."""
    try:
        page = wiki.page(query)
    except wiki.exceptions.PageError:
        return lex.wikipedia.not_found
    except wiki.exceptions.DisambiguationError as e:
        tools.save_results(inp, e.options, lambda x: wikipedia(inp, query=x))
        return lex.unclear(options=e.options)

    return lex.wikipedia.result(
        title=page.title, url=page.url, text=page.content)
项目:AceBot    作者:Run1e    | 项目源码 | 文件源码
def wikipedia(self, ctx, *, query):
        """Preview a Wikipedia article."""

        await ctx.trigger_typing()

        try:
            wiki = wikipedia.page(query)
        except:
            return await ctx.send('No results.')

        await self.embedwiki(ctx, wiki)
项目:Alfred    作者:JohnGiorgi    | 项目源码 | 文件源码
def wikipediaAction(message):
    """Makes the appropriate calls to the wikipedia API for answer wiki queries.

    Args:
        message: An incoming text message
        processer: Instance of NLProcessor class

    Returns:
        A message indicating what action was taking with the wikipedia API
    """
    # tokenize input
    tokens = tokenize.wordpunct_tokenize(message)
    # filter stopwords, additionally, remove 'wiki' or 'wikipedia'
    tokens_filtered = remove_stopwords(tokens)
    tokens_filtered = [token for token in tokens_filtered if token != 'wiki' and token != 'wikipedia']
    # join filtered message
    message = ' '.join(tokens_filtered)

    # for debugging/testing
    print("(Highly) processed input: ", message)

    # Get the wikipedia summary for the request
    try:
        summary = wikipedia.summary(message, sentences = 1)
        url = wikipedia.page(message).url
        answer = summary + "\nSee more here: " + url
        if len(answer) > 500:
            answer = answer[0:500] + "\nSee wikipedia for more..."
    except:
        # handle all errors
        answer = "Request was not found using Wikipedia. Be more specific?"

    return answer
项目:LDA-REST    作者:valentinarho    | 项目源码 | 文件源码
def download_single(wiki_page_name, only_summary=False, language='en'):
    """
    Download the content of a wikipedia page

    :param wiki_page_name: the name
    :param only_summary:
    :return:
    """

    wikipedia.set_lang(language)
    if only_summary:
        page = wikipedia.page(wiki_page_name)
        return page.content
    else:
        return wikipedia.summary(wiki_page_name)
项目:LDA-REST    作者:valentinarho    | 项目源码 | 文件源码
def download_all(wiki_page_names, only_summary=False, language='en'):
    contents = {}
    for pn in wiki_page_names:
        contents[pn] = download_single(pn, only_summary=only_summary, language=language)

    return contents


# TODO if starts with http or www get only the page name
项目:Jarvis    作者:sukeesh    | 项目源码 | 文件源码
def summary(query, sentences=0, chars=0):
    """Returns a plain text summary from the query's page."""
    try:
        return wikipedia.summary(query, sentences, chars)
    except wikipedia.exceptions.PageError:
        return "No page matches, try another item."
    except wikipedia.exceptions.DisambiguationError as error:
        return error.options[:5]
项目:Jarvis    作者:sukeesh    | 项目源码 | 文件源码
def content(title=None, pageid=None, auto_suggest=True, redirect=True, preload=False):
    """Returns plain text content of query's page, excluding images, tables and other data."""
    try:
        page = wikipedia.page(title)
        return page.content
    except wikipedia.exceptions.PageError:
        return "No page matches, try another item."
    except wikipedia.exceptions.DisambiguationError as error:
        return error.options[:5]
项目:wikiphilosophy    作者:adtac    | 项目源码 | 文件源码
def next_link(cur, done):
    try:
        g = wikipedia.page(cur).html()
    except wikipedia.exceptions.DisambiguationError as e:
        for op in e.options:
            if op not in done:
                g = wikipedia.page(op).html()
                break
    soup = BeautifulSoup(re.sub(r'\([^)]*\)', '', g), "html.parser")
    for para in soup.findAll("p"):
        flag = False
        for link in para.findAll("a"):
            flag = True
            if link.get("href").startswith("/wiki/") and link.get("title") not in done and link.contents[0].islower():
                return link.get("title")
项目:WikipediaQuiz    作者:NicholasMoser    | 项目源码 | 文件源码
def retrieve_random_passage(page, length):
    """Given a wikipedia page and length, retrieves a random passage of text from
    the content of the wikipedia page with the given length.
    """
    content = page.content
    content_length = len(content)
    if length > content_length:
        length = content_length - 1
    start = random.randrange(len(content) - length)
    end = start + length
    return content[start:end]
项目:tuxbot-bot    作者:outout14    | 项目源码 | 文件源码
def search_docubuntu(self, ctx, args):
        attends = await ctx.send("_Je te cherche ça {} !_".format(ctx.message.author.mention))
        html = urllib.request.urlopen("https://doc.ubuntu-fr.org/" + args).read()
        if "avez suivi un lien" in str(html):
           await attends.edit(content=":sob: Nooooon ! Cette page n'existe pas, mais tu peux toujours la créer : https://doc.ubuntu-fr.org/"+ args)
        else:
           await attends.delete()
           embed = discord.Embed(description="Voila j'ai trouvé ! Voici la page ramenant à votre recherche, toujours aussi bien rédigée :wink: : https://doc.ubuntu-fr.org/" + args, url='http://doc.ubuntu-fr.org/')
           embed.set_author(name="DocUbuntu-Fr", url='http://doc.ubuntu-fr.org/', icon_url='http://outout.tech/tuxbot/ubuntu.png')
           embed.set_thumbnail(url='http://outout.tech/tuxbot/ubuntu.png')
           embed.set_footer(text="Merci à ceux qui ont pris le temps d'écrire cette documentation")
           await ctx.send(embed=embed)
项目:falco    作者:nathan0    | 项目源码 | 文件源码
def randwiki(irc, source, msgtarget, args):
    rand = wikipedia.random(pages=1)
    url = wikipedia.page(rand).url
    irc.msg(msgtarget, "Random Article: {} - \x1d{}\x1d".format(rand, url))
    irc.msg(msgtarget, wikipedia.summary(rand, sentences=2, chars=250, auto_suggest=True))
项目:falco    作者:nathan0    | 项目源码 | 文件源码
def wiki(irc, source, msgtarget, args):
    try:
        url = wikipedia.page(args).url
        page = wikipedia.summary(wikipedia.search(args)[0], sentences=2, auto_suggest=True)
        irc.msg(msgtarget, page)
        irc.msg(msgtarget, "More at \x1d"+url)
    except wikipedia.exceptions.DisambiguationError as e:
        bot_commands["wiki"](irc, source, msgtarget, e.options[0])
    except wikipedia.exceptions.PageError:
        irc.msg(msgtarget, "No page could be found")
项目:jessy    作者:jessy-project    | 项目源码 | 文件源码
def ask_wikipedia(self, definition):
        '''
        Ask Wikipedia for the definition.

        :param definition:
        :return:
        '''
        # TODO: this method should run in a separate process, asynchronously

        is_exact = False
        out = []
        if not wikipedia:
            return is_exact, out

        page_titles = wikipedia.search(definition)
        page = None
        if page_titles:
            for page_title in page_titles:
                if page_title.lower() == definition:
                    try:
                        page = wikipedia.page(page_title)
                        is_exact = True
                    except DisambiguationError as ex:
                        out.append(Phrase().text('This can refer to a many things, such as {0}'.format(self.join_for_more(ex.options, limit=None))))
                        return is_exact, out

            if not page and 'disambiguation' not in page_titles[0]:
                try:
                    page = wikipedia.page(page_titles[0])
                except Exception as ex:
                    out.append(Phrase().text(str(ex)))

        if page and not out:
            out.append(Phrase().text(page.content.split('==')[0]
                                     .split('\n')[0]
                                     .encode('utf-8', 'ignore')).pause(1))
        return is_exact, out
项目:pyconjpbot    作者:pyconjp    | 项目源码 | 文件源码
def wikipedia_page(message, option, query):
    """
    Wikipedia??????????
    """
    if query == 'help':
        return

    # set language
    lang = 'ja'
    if option:
        _, lang = option.split('-')
    wikipedia.set_lang(lang)

    try:
        # search with query
        results = wikipedia.search(query)
    except:
        botsend(message, '??????? `{}` ???????'.format(lang))
        return

    # get first result
    if results:
        page = wikipedia.page(results[0])

        attachments = [{
            'fallback': 'Wikipedia: {}'.format(page.title),
            'pretext': 'Wikipedia: <{}|{}>'.format(page.url, page.title),
            'text': page.summary,
        }]
        botwebapi(message, attachments)
    else:
        botsend(message, '`{}` ??????????????'.format(query))
项目:quantulum    作者:marcolagi    | 项目源码 | 文件源码
def download_wiki():
    """Download WikiPedia pages of ambiguous units."""
    ambiguous = [i for i in l.UNITS.items() if len(i[1]) > 1]
    ambiguous += [i for i in l.DERIVED_ENT.items() if len(i[1]) > 1]
    pages = set([(j.name, j.uri) for i in ambiguous for j in i[1]])

    print
    objs = []
    for num, page in enumerate(pages):

        obj = {'url': page[1]}
        obj['_id'] = obj['url'].replace('https://en.wikipedia.org/wiki/', '')
        obj['clean'] = obj['_id'].replace('_', ' ')

        print '---> Downloading %s (%d of %d)' % \
              (obj['clean'], num + 1, len(pages))

        obj['text'] = wikipedia.page(obj['clean']).content
        obj['unit'] = page[0]
        objs.append(obj)

    path = os.path.join(l.TOPDIR, 'wiki.json')
    os.remove(path)
    json.dump(objs, open(path, 'w'), indent=4, sort_keys=True)

    print '\n---> All done.\n'


###############################################################################
项目:quantulum    作者:marcolagi    | 项目源码 | 文件源码
def wiki_test(page='CERN'):
    """Download a wikipedia page and test the parser on its content.

    Pages full of units:
        CERN
        Hubble_Space_Telescope,
        Herschel_Space_Observatory
    """
    content = wikipedia.page(page).content
    parsed = p.parse(content)
    parts = int(round(len(content) * 1.0 / 1000))

    print
    end_char = 0
    for num, chunk in enumerate(range(parts)):
        _ = os.system('clear')
        print
        quants = [j for j in parsed if chunk * 1000 < j.span[0] < (chunk + 1) *
                  1000]
        beg_char = max(chunk * 1000, end_char)
        text, end_char = embed_text(quants, beg_char, chunk, content)
        print COLOR2 % text
        print
        try:
            _ = raw_input('--------- End part %d of %d\n' % (num + 1, parts))
        except (KeyboardInterrupt, EOFError):
            return


###############################################################################
项目:ray    作者:ray-project    | 项目源码 | 文件源码
def get_new_article(self):
        # Get the next wikipedia article.
        article = wikipedia.page(self.title_stream.next()).content
        # Count the words and store the result.
        self.word_counts.append(Counter(article.split(" ")))
        self.num_articles_processed += 1
项目:cnschema    作者:cnschema    | 项目源码 | 文件源码
def wikify2(phrase, description=None):
    #wikification
    ret = {}
    wikiterm = wikipedia.search(phrase)
    for idx, term in enumerate(wikiterm[0:MAX_RESULT]):
        wikipage = wikipedia.page(term)
        ret["wikipedia_{}_url".format(idx)] = wikipage.url
        ret["wikipedia_{}_desc".format(idx)] = wikipedia.summary(term, sentences=1)

    return ret
项目:TwentyTwo    作者:EPITECH-2022    | 项目源码 | 文件源码
def wikipedia(self, context, lang: str = None, query: str = None):
        ''' Get a page from wikipedia and reply with an embed '''
        query = self.bot.get_text(context)
        if lang is not None:
            if lang.startswith('(') and lang.endswith(')'):
                query = query[len(lang) + 1:]
                lang = lang[1:-1]
            else:
                lang = None
        if query in [None, '', ' ']:
            await self.bot.doubt(context)
            return
        try:
            import wikipedia
            if lang is not None and lang in wikipedia.languages().keys():
                wikipedia.set_lang(lang)
            else:
                wikipedia.set_lang('en')
            page    = wikipedia.page(query)
            summary = page.summary
            if len(summary) > 1222: # totally arbitrary chosen number
                summary = summary[:1220] + '...'
            embed   = discord.Embed(title=page.title, description=summary, url=page.url)
            embed.set_footer(text=page.url)
            if self.bot.config['bleeding']:
                if len(page.images) > 0:
                    embed.set_image(url=page.images[0])
            await self.bot.say(embed=embed)
            await self.bot.replied(context)
        except wikipedia.PageError as e:
            await self.bot.reply('{}\nMake sure you search for page titles in the language that you have set.'.format(e))
            await self.bot.doubt(context)
        except KeyError:
            pass
        except wikipedia.DisambiguationError as e:
            msg = '```\n{}\n```'.format(e)
            await self.bot.doubt(context)
            await self.bot.say(msg)
项目:prox-server    作者:mozilla-mobile    | 项目源码 | 文件源码
def resolve(pageID):
    page = wikipedia.page(pageID, preload=True)
    return {
      "url": page.url,
      "summary": page.summary,
      "images": page.images,
    }

# --- SEARCH PARAMETERS --- #
# Run `pytest` when changing these parameters to ensure they still work.
项目:prox-server    作者:mozilla-mobile    | 项目源码 | 文件源码
def search(coord, place_name):
    """Finds the Wikipedia page corresponding to the given place.

    The current implementation requires Wikipedia geotags, meaning it'll miss:
    - Chains (Starbucks)
    - Corporate pages (Lagunitas, as opposed to the brewery site)
    - Area-based things (49-mile drive in SF)

    :param coord: is (latitude, longitude)
    :return: A wikipedia page title.
    """
    # We don't use the title arg of `wikipedia.geosearch`. It will return exact title matches, even if the geo location
    # does not match, so "Boulevard" will return a street rather than the restaurant (which is "Boulevard (restaurant)").
    wiki_page_titles = wikipedia.geosearch(*coord)
    return _match_place_name_to_wiki_page(place_name, wiki_page_titles)
项目:Onyx    作者:OnyxProject    | 项目源码 | 文件源码
def get_article(self):
        try:
            wikipedia.set_lang(self.lang)
            article = wikipedia.page(self.search)
            return article
        except Exception as e:
            logger.error('Getting wiki article error : ' + str(e))
            raise WikiException(str(e))
项目:good-or-bad    作者:osori    | 项目源码 | 文件源码
def getwikitext(title):
    try:
        print('Fetching data from Wikipedia...')
        page = wikipedia.page(title)
        text = page.content
    except wikipedia.exceptions.PageError:
        return 'PageError'

    return text
项目:JshBot-legacy    作者:jkchen2    | 项目源码 | 文件源码
def wikipedia_query(query, simple_result=False):
    if not query:
        return "Try searching for *something* next time, knucklehead."
    try:
        page = wikipedia.page(query, auto_suggest=True)
        if simple_result: # Just return the url of the found page
            return page.url
        else: # Return the first ~500 characters of the summary
            title = page.title
            summary = page.summary
            for i in range(0, (len(summary) if len(summary) < 500 else 500) - 1):
                if summary[i] == '=' and summary[i+1] == '=':
                    summary = summary[0:i]
                    break;
            if len(summary) >= 500:
                summary = summary[0:500]
                summary += ' ...*`[truncated]`*'
            return "***```{title}```***\n{summary}".format(title=title, summary=summary)
    except wikipedia.exceptions.PageError:
        raise bot_exception(WIKIPEDIA_EXCEPTION, 
            "Page doesn't exist. Trying for some suggestions...", '```{}```'.format(
            (wikipedia.suggest(query) if wikipedia.suggest(query) is not None else "None")))
    except wikipedia.exceptions.DisambiguationError as tp: # Try to get list of suggestions
        suggestions = wikipedia.search(query, results=5)
        if len(suggestions) > 0:
            formatted_suggestions = '```\n'
            for suggestion in suggestions:
                formatted_suggestions += '{}\n'.format(suggestion)
            formatted_suggestions += '```'
            raise bot_exception(WIKIPEDIA_EXCEPTION, "Query is too ambiguous. Here are some suggestions:", formatted_suggestions)
        else:
            raise bot_exception(WIKIPEDIA_EXCEPTION, "Query is too ambiguous. No suggestions found.")
项目:tulen    作者:detorto    | 项目源码 | 文件源码
def get_wiki_page(req):
        wikipedia.set_lang("ru")
        p = wikipedia.page(req)
        return p
项目:tulen    作者:detorto    | 项目源码 | 文件源码
def save_images(page):
    local_images = []
    counter = 0
    for i,img in enumerate(page.images):
        fname = img.split("/")[-1];
        if img.endswith(IMAGES_EXT) and "Aquote" not in fname and "Commons-logo" not in fname and "Wiktionary" not in fname:
            print fname 
            urllib.urlretrieve(img, "./files/"+fname)
            local_images.append("./files/"+fname)
            counter += 1
            if counter >= IMAGES_COUNT:
                break
    return local_images
项目:eatradish_haoyebot    作者:eatradish    | 项目源码 | 文件源码
def pixiv():
    url = 'https://public-api.secure.pixiv.net/v1/ranking/all?image_sizes=px_128x128%2Cpx_480mw%2Clarge&include_stats=true&page=1&profile_image_sizes=px_170x170%2Cpx_50x50&mode=daily&include_sanity_level=true&per_page=50'
    headers = {"Host": "public-api.secure.pixiv.net", "Authorization": "Bearer WHDWCGnwWA2C8PRfQSdXJxjXp0G6ULRaRkkd6t5B6h8", "Accept-Encoding": "gzip, deflate", "Accept": "*/*", "Accept-Language": "zh-cn", "Connection": "keep-alive", "Proxy-ConnectAion": "keep-alive", "User-Agent": "PixivIOSApp/5.6.0", "Referer": "http://spapi.pixiv.net/"}
    r = requests.get(url, headers = headers)
    j = json.loads(r.text)
    num = random.randint(0, 49)
    photo_url = j['response'][0]['works'][num]['work']['image_urls']['large']
    pixiv_url = 'https://www.pixiv.net/member_illust.php?mode=medium&illust_id={}'.format(urllib.parse.urlsplit(photo_url).path.split('/')[-1].replace('_p0.jpg', '').replace('_p0.png', ''))
    return {'photo': photo_url, 'pixiv': pixiv_url}
项目:WikipediaQuiz    作者:NicholasMoser    | 项目源码 | 文件源码
def wikipedia_quiz(number_of_articles, passage_length):
    """Generates a multiple choice quiz to identify the correct wikipedia article that
    a random passage is pulled from. The number of articles determines how many choices
    you must pick from. The passage length determines the number of characters that the
    random passage will be.
    """
    print('*** Wikipedia Quiz ***')
    logging.info('Quiz is starting')
    random_articles = wikipedia.random(number_of_articles)
    logging.debug('Random articles: %s', str(random_articles).encode('utf-8'))
    correct_article_index = random.randrange(number_of_articles)
    page_retrieved = False
    while not page_retrieved:
        try:
            correct_article = random_articles[correct_article_index]
            correct_page = wikipedia.page(correct_article)
            page_retrieved = True
        except wikipedia.exceptions.DisambiguationError:
            # Wikipedia provides options to choose from, but if we pick one, the title will be
            # much more descriptive (particularly by using parenthesis like so). This usually
            # ends up making the guessing too easy. Let's just reroll and put the new random
            # article in the place of the old one.
            new_random_article = wikipedia.random()
            random_articles[correct_article_index] = new_random_article

    # Try to obtain a good passage
    random_passage = retrieve_random_passage(correct_page, passage_length)
    retry = 0
    while is_passage_unfair(random_passage, correct_article) and retry < RETRY_AMOUNT_MAX:
        logging.info('Passage is unfair, generating a new one...')
        random_passage = retrieve_random_passage(correct_page, passage_length)
        retry += 1
    if retry >= RETRY_AMOUNT_MAX:
        print('Too many retries for the passage...')
        logging.error('Too many retries for the passage...')
        return False

    # Print info to user
    print('...%s...' % random_passage)
    encode_utf8 = sys.version_info.major == 2 # Hack support for Python 2
    for index, random_article in enumerate(random_articles):
        if encode_utf8:
            random_article = random_article.encode('utf-8')
        print('%d: %s' % (index, random_article))

    # Handle answer
    answer = request_answer(number_of_articles)
    if answer == str(correct_article_index):
        print('Correct!')
        logging.info('Correct, answer was %d', correct_article_index)
    else:
        print('Incorrect, answer was: %d' % correct_article_index)
        logging.info('Incorrect, answer was: %d', correct_article_index)
    logging.info('Quiz is ending')
    return True
项目:tuxbot-bot    作者:outout14    | 项目源码 | 文件源码
def search_wikipedia(self, ctx: commands.Context, args):
        """Fait une recherche sur wikipd"""

        wait = await ctx.send("_Je cherche..._")
        results = wikipedia.search(args)
        nbmr = 0
        mmssgg = ""

        for value in results:
            nbmr = nbmr + 1
            mmssgg = mmssgg + "**{}**: {} \n".format(str(nbmr), value)

        em = discord.Embed(title='Résultats de : ' + args, description = mmssgg, colour=0x4ECDC4)
        em.set_thumbnail(url = "https://upload.wikimedia.org/wikipedia/commons/2/26/Paullusmagnus-logo_%28large%29.png")
        await wait.delete()

        sending = ["1?", "2?", "3?", "4?", "5?", "6?", "7?", "8?", "9?", "??"]

        def check(reaction, user):
            return user == ctx.author and reaction.emoji in sending and reaction.message.id == msg.id

        async def waiter(future: asyncio.Future):
            reaction, user = await self.bot.wait_for('reaction_add', check=check)
            future.set_result(reaction.emoji)

        emoji = asyncio.Future()
        self.bot.loop.create_task(waiter(emoji))

        msg = await ctx.send(embed=em)
        for e in sending:
            await msg.add_reaction(e)
            if emoji.done():
                break

        while not emoji.done():
            await asyncio.sleep(0.1)

        sPage = int(sending.index(emoji.result()))

        args_ = results[sPage]

        try:
            await msg.delete()
            await ctx.trigger_typing()
            wait = await ctx.send(ctx.message.author.mention + " ah ok sympa cette recherche, je l'effectue de suite !")
            wp = wikipedia.page(args_)
            wp_contenu = wp.summary[:200] + "..."
            em = discord.Embed(title='Wikipedia : ' + wp.title, description = "{} \n_Lien_ : {} ".format(wp_contenu, wp.url), colour=0x9B59B6)
            em.set_author(name="Wikipedia", url='http://wikipedia.org', icon_url='https://upload.wikimedia.org/wikipedia/commons/2/26/Paullusmagnus-logo_%28large%29.png')
            em.set_thumbnail(url = "https://upload.wikimedia.org/wikipedia/commons/2/26/Paullusmagnus-logo_%28large%29.png")
            em.set_footer(text="Merci à eux de nous fournir une encyclopédie libre !")
            await wait.delete()
            await ctx.send(embed=em)

        except wikipedia.exceptions.PageError: #TODO : A virer dans l'event on_error
            await ctx.send(":open_mouth: Une **erreur interne** est survenue, si cela ce reproduit contactez votre administrateur ou faites une Issue sur ``github`` !")
项目:copaspedia    作者:selesdepselesnul    | 项目源码 | 文件源码
def _extract_from_wiki(self):
        title = self.title_line_edit.text()
        if title:
            page = self.page_combo_box.currentText()
            wikipedia.set_lang(self.lang_combo_box.currentText())
            self.load_progressbar.setMinimum(0)
            self.load_progressbar.setMaximum(0)

            class ProgressThread(QThread, QWidget):

                content_link_arrived = pyqtSignal([list])
                content_text_arrived = pyqtSignal(['QString'])
                content_image_arrived = pyqtSignal([list, 'QString'])
                error_occurred = pyqtSignal()
                valid_images = []

                def run(self):
                    try:
                        wiki = wikipedia.page(title=title)
                        f = open('templates/template.html')
                        if page == 'Content':
                            self.content_text_arrived.emit(wiki.content)
                        elif page == 'Images':

                            print(wiki.images)

                            self.des_dir = Preferences.output_path + '/' + title 
                            self.valid_images = []
                            if not os.path.exists(self.des_dir):
                                print(self.des_dir)
                                os.mkdir(self.des_dir)   

                            for i in wiki.images:
                                if PurePath(i).suffix in Preferences.valid_image_formats:
                                    print(i)
                                    print(self.des_dir)
                                    wget.download(i, out=self.des_dir)
                                    self.valid_images.append(i)
                            self.content_image_arrived.emit(self.valid_images, self.des_dir)

                        elif page == 'Summary':
                            self.content_text_arrived.emit(wiki.summary)
                        elif page == 'Images Links':
                            self.content_link_arrived.emit(wiki.images)
                        elif page == 'References Links':
                            self.content_link_arrived.emit(wiki.references)


                    except:
                        self.error_occurred.emit()

            self.progress_thread = ProgressThread()
            self.progress_thread.content_link_arrived.connect(self.set_content_link)
            self.progress_thread.content_text_arrived.connect(self.set_content_text)
            self.progress_thread.content_image_arrived.connect(self.set_content_image)
            self.progress_thread.error_occurred.connect(self.handle_error_occurred)
            self.progress_thread.start()
        else:
            self.content_text_browser.clear()
            self.content_text_browser.setEnabled(False)
项目:JARVIS-on-Messenger    作者:swapagarwal    | 项目源码 | 文件源码
def process(input, entities):
    output = {}
    try:
        query = entities['wiki'][0]['value']
        data = wikipedia.page(query)
        output['input'] = input
        template = TextTemplate('Wikipedia summary of ' + data.title + ':\n' + data.summary)
        text = template.get_text()
        template = ButtonTemplate(text)
        template.add_web_url('Wikipedia Link', data.url)
        output['output'] = template.get_message()
        output['success'] = True
    except wikipedia.exceptions.DisambiguationError as e:
        template = GenericTemplate()
        template.set_image_aspect_ratio_to_square()
        image_url = 'https://en.wikipedia.org/static/images/project-logos/enwiki-2x.png'
        pageids = set()
        for option in e.options:
            try:
                data = wikipedia.page(option)
                if data.pageid in pageids:
                    continue
                pageids.add(data.pageid)
                buttons = ButtonTemplate()
                buttons.add_web_url('Wikipedia Link', data.url)
                payload = {
                    'intent': 'wiki',
                    'entities': {
                        'wiki': [
                            {
                                'value': option
                            }
                        ]
                    }
                }
                buttons.add_postback('Wikipedia Summary', payload)
                template.add_element(title=data.title, item_url=data.url, image_url=image_url,
                                     buttons=buttons.get_buttons())
            except (wikipedia.exceptions.PageError, wikipedia.exceptions.DisambiguationError):
                pass  # Some suggestions don't map to a page; skipping them..
        output['input'] = input
        output['output'] = template.get_message()
        output['success'] = True
    except:
        error_message = 'I couldn\'t find any wikipedia results matching your query.'
        error_message += '\nPlease ask me something else, like:'
        error_message += '\n  - wikipedia barack'
        error_message += '\n  - html wiki'
        error_message += '\n  - who is sachin tendulkar'
        output['error_msg'] = TextTemplate(error_message).get_message()
        output['success'] = False
    return output
项目:Rero    作者:voqz    | 项目源码 | 文件源码
def process(input, entities):
    output = {}
    try:
        query = entities['wiki'][0]['value']
        data = wikipedia.page(query)
        output['input'] = input
        template = TextTemplate('Wikipedia summary of ' + data.title + ':\n' + data.summary)
        text = template.get_text()
        template = ButtonTemplate(text)
        template.add_web_url('Wikipedia Link', data.url)
        output['output'] = template.get_message()
        output['success'] = True
    except wikipedia.exceptions.DisambiguationError as e:
        template = GenericTemplate()
        image_url = 'https://en.wikipedia.org/static/images/project-logos/enwiki-2x.png'
        pageids = set()
        for option in e.options:
            try:
                data = wikipedia.page(option)
                if data.pageid in pageids:
                    continue
                pageids.add(data.pageid)
                buttons = ButtonTemplate()
                buttons.add_web_url('Wikipedia Link', data.url)
                payload = {
                    'intent': 'wiki',
                    'entities': {
                        'wiki': [
                            {
                                'value': option
                            }
                        ]
                    }
                }
                buttons.add_postback('Wikipedia Summary', json.dumps(payload))
                template.add_element(title=data.title, item_url=data.url, image_url=image_url, buttons=buttons.get_buttons())
            except (wikipedia.exceptions.PageError, wikipedia.exceptions.DisambiguationError):
                pass # Some suggestions don't map to a page; skipping them..
        output['input'] = input
        output['output'] = template.get_message()
        output['success'] = True
    except:
        error_message = 'I couldn\'t find any wikipedia results matching your query.'
        error_message += '\nPlease ask me something else, like:'
        error_message += '\n  - wikipedia barack'
        error_message += '\n  - html wiki'
        error_message += '\n  - who is sachin tendulkar'
        output['error_msg'] = TextTemplate(error_message).get_message()
        output['success'] = False
    return output