本文整理汇总了Python中wikipedia.page方法的典型用法代码示例。如果您正苦于以下问题:Python wikipedia.page方法的具体用法?Python wikipedia.page怎么用?Python wikipedia.page使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类wikipedia
的用法示例。
在下文中一共展示了wikipedia.page方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: search_wiki
# 需要导入模块: import wikipedia [as 别名]
# 或者: from wikipedia import page [as 别名]
def search_wiki(keywords, number_of_search, wiki_pages):
suggestion = False
for word in range(0, len(keywords) - 1):
# print(keywords[word], ">>")
result_set = wikipedia.search(keywords[word], number_of_search, suggestion)
for term in result_set:
try:
page = wikipedia.page(term, preload=False)
page_title = page.title
# page_summary = page.summary
page_content = page.content
wiki_pages[page_title] = page_content
except wikipedia.exceptions.DisambiguationError as error:
pass
except wikipedia.exceptions.PageError as error:
pass
# print(error.options)
# print(page_title, len(page_content), type(page_content))
return wiki_pages
示例2: get_articles
# 需要导入模块: import wikipedia [as 别名]
# 或者: from wikipedia import page [as 别名]
def get_articles(language, no_words, max_no_articles, search, **kwargs):
""" Retrieve articles from Wikipedia """
wikipedia.set_rate_limiting(True) # be polite
wikipedia.set_lang(language)
if search is not None:
titles = wikipedia.search(search, results = max_no_articles)
else:
titles = wikipedia.random(pages = max_no_articles)
articles = []
current_no_words = 0
for title in titles:
print("INFO: loading {}".format(title))
page = wikipedia.page(title=title)
content = page.content
article_no_words = len(content.split())
current_no_words += article_no_words
print("INFO: article contains {} words".format(article_no_words))
articles.append((title, content))
if current_no_words >= no_words:
break
return articles
示例3: wiki
# 需要导入模块: import wikipedia [as 别名]
# 或者: from wikipedia import page [as 别名]
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', '<br /><br />')
source = _('<i>source: <a href="{}">{}</a></i>').format(page.url, page.url)
html_text = '<b>"{}"</b><br /><br />{}<br /><br />{}'.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", "<br />")
html_text = "<i>{}</i>".format(exception_text)
yield from bot.coro_send_message(event.conv, html_text)
示例4: wiki
# 需要导入模块: import wikipedia [as 别名]
# 或者: from wikipedia import page [as 别名]
def wiki(bot, update, args):
try:
topic = ""
for arg in args:
topic += arg + " "
summary = wikipedia.summary(topic, sentences = 30)
page = wikipedia.page(topic)
extra = "\nFor more details visit " + page.url
summary += extra
bot.sendChatAction(chat_id = update.message.chat_id, action = ChatAction.TYPING)
bot.sendMessage(chat_id = update.message.chat_id, parse_mode=ParseMode.HTML, text = summary)
except wikipedia.exceptions.DisambiguationError as e:
error = "Please be more specific with your search query as there are a couple of other options meaning the same."
for options in e.options:
error += options.decode("utf-8","ignore")+'\n'
bot.sendChatAction(chat_id = update.message.chat_id, action = ChatAction.TYPING)
bot.sendMessage(chat_id = update.message.chat_id, text = error)
except wikipedia.exceptions.PageError:
error = "No messages could be found with the topic you entered!"
bot.sendChatAction(chat_id = update.message.chat_id, action = ChatAction.TYPING)
bot.sendMessage(chat_id = update.message.chat_id, text = error)
示例5: cli
# 需要导入模块: import wikipedia [as 别名]
# 或者: from wikipedia import page [as 别名]
def cli(google,wiki):
browser = mechanize.Browser()
browser.set_handle_robots(False) #Allows everything to be written
cookies = mechanize.CookieJar()
browser.set_cookiejar(cookies)
browser.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.517.41 Safari/534.7')]
browser.set_handle_refresh(False) #Sometimes hangs without this
if(google):
query = raw_input("Enter the topic you want to search about: ")
for link in search(query, tld="co.in", num=10, stop=1, pause=2):
print link
if(wiki):
wiki_topic = raw_input('Enter the topic you want to read about: ')
result = wikipedia.page(title=wiki_topic,auto_suggest=True,redirect=True, preload=False)
wiki_content = result.content
print wiki_content
示例6: wikipedia
# 需要导入模块: import wikipedia [as 别名]
# 或者: from wikipedia import page [as 别名]
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))
示例7: translate
# 需要导入模块: import wikipedia [as 别名]
# 或者: from wikipedia import page [as 别名]
def translate(to_translate, to_language="auto", language="auto"):
bahasa_awal = "auto"
bahasa_tujuan = to_language
kata = to_translate
url = 'https://translate.google.com/m?sl=%s&tl=%s&ie=UTF-8&prev=_m&q=%s' % (bahasa_awal, bahasa_tujuan, kata.replace(" ", "+"))
agent = {'User-Agent':'Mozilla/5.0'}
cari_hasil = 'class="t0">'
request = urllib2.Request(url, headers=agent)
page = urllib2.urlopen(request).read()
result = page[page.find(cari_hasil)+len(cari_hasil):]
result = result.split("<")[0]
return result
示例8: download_page
# 需要导入模块: import wikipedia [as 别名]
# 或者: from wikipedia import page [as 别名]
def download_page(url):
version = (3,0)
cur_version = sys.version_info
if cur_version >= version: #If the Current Version of Python is 3.0 or above
import urllib,request #urllib library for Extracting web pages
try:
headers = {}
headers['User-Agent'] = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"
req = urllib,request.Request(url, headers = headers)
resp = urllib,request.urlopen(req)
respData = str(resp.read())
return respData
except Exception as e:
print(str(e))
else: #If the Current Version of Python is 2.x
import urllib2
try:
headers = {}
headers['User-Agent'] = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.27 Safari/537.17"
req = urllib2.Request(url, headers = headers)
response = urllib2.urlopen(req)
page = response.read()
return page
except:
return"Page Not found"
#Finding 'Next Image' from the given raw page
示例9: _images_get_all_items
# 需要导入模块: import wikipedia [as 别名]
# 或者: from wikipedia import page [as 别名]
def _images_get_all_items(page):
items = []
while True:
item, end_content = _images_get_next_item(page)
if item == "no_links":
break
else:
items.append(item) #Append all the links in the list named 'Links'
time.sleep(0.1) #Timer could be used to slow down the request for image downloads
page = page[end_content:]
return items
示例10: download_pages
# 需要导入模块: import wikipedia [as 别名]
# 或者: from wikipedia import page [as 别名]
def download_pages():
bonus_questions = BonusQuestionDatabase().all_questions()
train_answers = set()
for q in bonus_questions.values():
train_answers.update(q.pages)
if os.path.isfile(BONUS_ANSWER_PAGES):
with open(BONUS_ANSWER_PAGES, 'rb') as f:
try:
pages = pickle.load(f)
print('loaded {} pages'.format(len(pages)))
except EOFError:
pages = dict()
else:
pages = dict()
train_answers = train_answers - set(pages.keys())
for answer in tqdm(train_answers):
if answer in pages:
continue
try:
page = wikipedia.page(answer)
except (DisambiguationError, PageError, ConnectionError) as e:
if isinstance(e, DisambiguationError):
pages[answer] = None
continue
if isinstance(e, PageError):
pages[answer] = None
continue
if isinstance(e, ConnectionError):
break
try:
pages[answer] = [page.title, page.content, page.links,
page.summary, page.categories, page.url, page.pageid]
except ConnectionError:
break
with open(BONUS_ANSWER_PAGES, 'wb') as f:
pickle.dump(pages, f)
示例11: wikipedia_page
# 需要导入模块: import wikipedia [as 别名]
# 或者: from wikipedia import page [as 别名]
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))
示例12: summary
# 需要导入模块: import wikipedia [as 别名]
# 或者: from wikipedia import page [as 别名]
def summary(self, 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]
示例13: content
# 需要导入模块: import wikipedia [as 别名]
# 或者: from wikipedia import page [as 别名]
def content(
self,
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]
示例14: searchWikipedia
# 需要导入模块: import wikipedia [as 别名]
# 或者: from wikipedia import page [as 别名]
def searchWikipedia(target):
try:
wikipedia.set_lang("es")
d0 = wikipedia.search(target)
if d0:
print()
print("|----[INFO][WIKIPEDIA][>] ")
print(" |----[INFO][SEARCH][>] ")
print(" - Resultados encontrados: ")
for r in d0:
print(" - " + r)
else:
print("|----[INFO][WIKIPEDIA][>] No aparecen resultados en WIKIPEDIA.")
except:
print("[!][WARNING][WIKIPEDIA][>] Error en la API...")
try:
d1 = wikipedia.page(target)
linksWIKI = d1.links
urlWIKI = d1.url
if d1:
print(" |----[INFO][TAGS][>] ")
for l in linksWIKI:
print(" - " + l)
print("|----[FUENTES][WIKIPEDIA][>] ")
print(" - " + urlWIKI)
config.wikipediaData_list.append(urlWIKI)
else:
print("|----[INFO][WIKIPEDIA][>] No aparecen resultados en WIKIPEDIA.")
except:
print("[!][WARNING][WIKIPEDIA][>] Error en la API o no aparecen resultados...")
#Funciones para buscar en Youtube
示例15: get_next
# 需要导入模块: import wikipedia [as 别名]
# 或者: from wikipedia import page [as 别名]
def get_next(self):
if self.done:
return None # Source exhausted
while True:
if self.article_done:
try: # Try next title
next_title = next(self.title_reader)
except StopIteration:
self.done = True # Source exhausted
return None
# Get next article
logger.debug("Next article: {}".format(next_title))
article = wikipedia.page(next_title).content
# Split article in sentences
self.sentences = iter(article.split("."))
self.article_done = False
try: # Try next sentence
sentence = next(self.sentences)
logger.debug("Next sentence: {}".format(sentence))
return sentence
except StopIteration:
self.article_done = True
# Splits input line into words and
# outputs records of the form (word,1)