本文整理汇总了Python中wikipedia.search方法的典型用法代码示例。如果您正苦于以下问题:Python wikipedia.search方法的具体用法?Python wikipedia.search怎么用?Python wikipedia.search使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类wikipedia
的用法示例。
在下文中一共展示了wikipedia.search方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: search_wiki
# 需要导入模块: import wikipedia [as 别名]
# 或者: from wikipedia import search [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 search [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: main
# 需要导入模块: import wikipedia [as 别名]
# 或者: from wikipedia import search [as 别名]
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-n", "--max-no-articles", type = int, default=10,
help = "maximum number of articles to download")
parser.add_argument("-w", "--no-words", type = int, default=1000000,
help = "target number of words")
parser.add_argument("-s", "--search",
help = "if specified will use this search term")
parser.add_argument("language",
help = "2 letter language code")
parser.add_argument("output", type = argparse.FileType('w'),
help = "output file")
args = parser.parse_args()
articles = get_articles(**vars(args))
corpusxml = articles2xml(articles)
xmlstr = lxml.etree.tostring(corpusxml,
pretty_print=True,
xml_declaration=True,
encoding='utf-8')
args.output.write(xmlstr.decode('utf-8'))
示例4: __call__
# 需要导入模块: import wikipedia [as 别名]
# 或者: from wikipedia import search [as 别名]
def __call__(self, jarvis, s):
k = s.split(' ', 1)
if len(k) == 1:
jarvis.say(
"Do you mean:\n1. wiki search <subject>\n2. wiki summary <subject>\n3. wiki content <subject>")
else:
data = None
if k[0] == "search":
data = self.search(" ".join(k[1:]))
elif k[0] == "summary":
data = self.summary(" ".join(k[1:]))
elif k[0] == "content":
data = self.content(" ".join(k[1:]))
else:
jarvis.say("I don't know what you mean")
return
if isinstance(data, list):
print("\nDid you mean one of these pages?\n")
for d in range(len(data)):
print(str(d + 1) + ": " + data[d])
else:
print("\n" + data)
示例5: is_search
# 需要导入模块: import wikipedia [as 别名]
# 或者: from wikipedia import search [as 别名]
def is_search(event):
'''Determine whether it's a search command'''
command = event["command"]
if "search" in event["verbs"]:
return True
question_words = [
"what",
"when",
"why",
"how",
"who",
"are",
"is"
]
first_word = command.split(" ")[0].lower()
log.debug("First word in command is {0}".format(first_word))
if first_word in question_words:
return True
return False
示例6: main
# 需要导入模块: import wikipedia [as 别名]
# 或者: from wikipedia import search [as 别名]
def main(data):
'''Start the search'''
response = {"text": None, "data":{}, "type": "success"}
query = data["command"]
log.info("In main search function with query {0}".format(query))
db = data["db"]
answer = False
wolfram_key = tools.load_key("wolfram", db)
wolfram_response = search_wolfram(query, wolfram_key)
# If it found an answer answer will be set to that, if not it'll still be false
answer = wolfram_response
if answer:
response["text"] = answer
else:
response["text"]=search_google(query)
return response
示例7: search_google_
# 需要导入模块: import wikipedia [as 别名]
# 或者: from wikipedia import search [as 别名]
def search_google_(target):
engine = Google()
results = engine.search("'" + target + "'")
for r in results:
print ("|--[INFO][GOOGLE][RESULTS][>] " + r["title"] + " | " + r["text"] + " | " + r["link"])
try:
tsd, td, tsu = extract(r["link"])
domain = td + '.' + tsu
web = requests.get(r["link"], timeout=3)
print ("|----[INFO][WEB][HTTP CODE][>] " + str(web.status_code) + "\n")
if web.status_code >= 200 or web.status_code < 300:
if not domain in config.BL_parserPhone:
TEXT = er.remove_tags(str(web.text))
parser.parserMAIN(TEXT)
except Exception as e:
print ("|----[ERROR][HTTP CONNECTION][>] " + str(e))
示例8: fetch_wiki_content
# 需要导入模块: import wikipedia [as 别名]
# 或者: from wikipedia import search [as 别名]
def fetch_wiki_content(num_threads, taxid2wikidict, taxid2wikicontent, id2namedict):
''' Fetch wikipedia content based on taxid2wikidict '''
threads = []
semaphore = threading.Semaphore(num_threads)
mutex = TraceLock("fetch_wiki_content", threading.RLock())
for taxid, url in taxid2wikidict.items():
m = re.search(r"curid=(\d+)", url)
pageid = None
if m:
pageid = m[1]
name = id2namedict.get(taxid)
if pageid or name:
semaphore.acquire()
t = threading.Thread(
target=PipelineStepFetchTaxInfo.
get_wiki_content_for_page,
args=[taxid, pageid, name, taxid2wikicontent, mutex, semaphore]
)
t.start()
threads.append(t)
for t in threads:
t.join()
示例9: wikipedia_page
# 需要导入模块: import wikipedia [as 别名]
# 或者: from wikipedia import search [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))
示例10: search
# 需要导入模块: import wikipedia [as 别名]
# 或者: from wikipedia import search [as 别名]
def search(self, query, count=10, suggestion=False):
"""Do a Wikipedia search for a query, returns a list of 10 related items."""
items = wikipedia.search(query, count, suggestion)
if isinstance(items, list) and items:
return items
return "No articles with that name, try another item."
示例11: playMusic
# 需要导入模块: import wikipedia [as 别名]
# 或者: from wikipedia import search [as 别名]
def playMusic(query):
# get YouTube list
pattern = re.compile('([^\s\w]|_)+')
b_string = re.sub(pattern, '', query)
phrase=b_string
pattern = re.compile("\\b(some|play)\\W", re.I)
query = [pattern.sub("", phrase)]
# get YouTube list
query = query[0]
print query
url = "https://www.googleapis.com/youtube/v3/search?part=snippet&key="+keyring.get_password('google','api_secret')+"&q="+urllib.quote_plus(query)+"&type=video"
response = urllib2.urlopen(url)
jsonResp = response.read()
decoded = json.loads(jsonResp)
#os.system('echo \''+url+'\' > url.txt') #for debugging
url = 'http://youtube.com/watch?v=' + decoded['items'][0]['id']['videoId']
theSongName = decoded['items'][0]['snippet']['title']
pattern = re.compile("([^a-zA-Z\d\s:,.']|_)+")
theSongName = re.sub(pattern, '', theSongName)
#for x in range(1,len(decoded['items'])):
#url = url + ' ' + 'http://youtube.com/watch?v=' + decoded['items'][x]['id']['videoId']
permission = audio_cortex.getUserPermission("Do you want to hear " + theSongName)
if permission:
vlc = 'cvlc --no-video --volume 270 -A alsa,none --alsa-audio-device hw:1' + ' ' + url + ' --play-and-exit &'
print url
os.system(vlc)
print "started music.."
return "Sure I'll play " + theSongName
else:
return "Okay, I will play nothing."
# Look up declarative knowledge with Wolfram
示例12: wikipediaLookUp
# 需要导入模块: import wikipedia [as 别名]
# 或者: from wikipedia import search [as 别名]
def wikipediaLookUp(a_string,num_sentences):
print a_string
pattern = re.compile('([^\s\w]|_)+')
b_string = re.sub(pattern, '', a_string)
phrase=b_string
print phrase
pattern = re.compile("\\b(lot|lots|a|an|who|can|you|what|is|info|somethings|whats|have|i|something|to|know|like|Id|information|about|tell|me)\\W", re.I)
phrase_noise_removed = [pattern.sub("", phrase)]
print phrase_noise_removed[0]
a = wikipedia.search(phrase_noise_removed[0])
print a[0]
the_summary = (wikipedia.summary(a[0], sentences=num_sentences))
print the_summary
return the_summary
示例13: searchWikipedia
# 需要导入模块: import wikipedia [as 别名]
# 或者: from wikipedia import search [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
示例14: search_dogpile_
# 需要导入模块: import wikipedia [as 别名]
# 或者: from wikipedia import search [as 别名]
def search_dogpile_(target):
engine = Dogpile()
results = engine.search("'" + target + "'")
for r in results:
print ("|--[INFO][DOGPILE][RESULTS][>] " + r["title"] + " | " + r["text"] + " | " + r["link"])
try:
web = requests.get(r["link"], timeout=3)
print ("|----[INFO][WEB][HTTP CODE][>] " + str(web.status_code) + "\n")
if web.status_code >= 200 or web.status_code < 300:
TEXT = er.remove_tags(str(web.text))
parser.parserMAIN(TEXT)
except Exception as e:
print ("|----[ERROR][HTTP CONNECTION][>] " + str(e))
示例15: who_is
# 需要导入模块: import wikipedia [as 别名]
# 或者: from wikipedia import search [as 别名]
def who_is(query, session_id="general"):
try:
return wikipedia.summary(query)
except requests.exceptions.SSLError:
return "Sorry I could not search online due to SSL error"
except:
pass
for new_query in wikipedia.search(query):
try:
return wikipedia.summary(new_query)
except:
pass
return "Sorry I could not find any data related to '%s'" % query