本文整理汇总了Python中wikipedia.random函数的典型用法代码示例。如果您正苦于以下问题:Python random函数的具体用法?Python random怎么用?Python random使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了random函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: search
def search(self):
""" Takes value from text fields and uses the wiki algorithm"""
self.text.delete(0.0, END)
# Assigns start to random if empty
x = self.start.get()
y = self.end.get()
if x == '':
while True:
try:
x = wikipedia.random(1)
self.text.insert(0.0, 'Random start is %s.\n' % x)
break
except UnicodeEncodeError:
pass
# Assigns end to random if empty
if y == '':
while True:
try:
y = wikipedia.random(1)
self.text.insert(0.0, 'Random end is %s.\n' % y)
break
except UnicodeEncodeError:
pass
self.text.insert(0.0, 'Start is %s.\nTarget is %s.\n' % (x,y))
示例2: get_random_articles_v2
def get_random_articles_v2():
"""Retrieves random articles until the user types 'stop' """
ans = input('Press enter to continue or stop to stop: ')
while ans != 'stop':
try:
print(wikipedia.summary(wikipedia.random()))
print()
ans = input('Press enter to continue or stop to stop: ')
except wikipedia.exceptions.DisambiguationError:
print(wikipedia.summary(wikipedia.random()))
print()
ans = input('Press enter to continue or stop to stop: ')
示例3: getPage
def getPage():
rpage = wikipedia.page(wikipedia.random(1))
while len(rpage.content) <= minlength:
try:
#TODO: Exception for lists
rpage = wikipedia.page(wikipedia.random(1))
except wikipedia.exceptions.DisambiguationError as e:
print 'ERROR'
rpage = wikipedia.page(e.options[0])
else:
print 'MISS'
rpage = wikipedia.page(wikipedia.random(1))
return rpage
示例4: get_random_page
def get_random_page():
# https://wikipedia.readthedocs.org/en/latest/quickstart.html
random_title = wikipedia.random(pages=1)
random_page = None
while not random_page:
try:
random_page = wikipedia.page(title=random_title)
except wikipedia.PageError:
random_title = wikipedia.random(pages=1)
random_page = None
except wikipedia.DisambiguationError as e:
random_title = random.choice(e.options)
random_page = None
return random_page
示例5: wikipedia_random
def wikipedia_random(self, n):
print "Downloading pages from Wikipedia. This may take a moment..."
f = open(self.inf, "w")
pages = []
# get a list of random wikipedia pages.
# wikipedia.random can only get 10 at a time, so call it so many times
for x in range(n/10):
pages += (wikipedia.random(10))
if n%10 > 0:
pages += (wikipedia.random(n%10))
log.info("Found {} random wikipedia pages".format(len(pages)))
# get the summary info from all the pages.
# this is a lot of pages, so can take some time
for page in pages:
info = None
try:
log.debug("Getting page "+page)
info = wikipedia.page(page)
# if the request threw a disambiguation error, try to get the first suggestion
# if that files, just give up on that request. we didn't want it that much anyway
except wikipedia.exceptions.DisambiguationError as e:
try:
log.debug("Getting page "+page)
info = wikipedia.page(e.options[0])
except wikipedia.exceptions.DisambiguationError as e:
continue
except (wikipedia.exceptions.HTTPTimeoutError, wikipedia.exceptions.PageError):
continue
except (wikipedia.exceptions.HTTPTimeoutError, wikipedia.exceptions.PageError):
continue
# save the summary. if it exceeds 127 characters, try to truncate by the first sentence end.
# if that fails, write it anyway, but it will give warning when it generates the config
s = info.summary.encode("ascii", "ignore").replace("\n", " ")
if len(s) < 127:
log.info("Wrote line with no edits")
f.write(s + "\n")
else:
if s[:127].rfind(". ") != -1:
f.write(s[:s[:127].rfind(". ")+1] + "\n")
log.info("Summary too long, but was truncated")
else:
f.write(s + "\n")
log.info("Summary too long and couldn't truncate")
f.close()
示例6: __init__
def __init__(self):
try:
self.page = wikipedia.page((wikipedia.random(pages=1)))
self.name = self.page.title
self.summary = self.page.summary
except wikipedia.DisambiguationError:
Wiki_page()
示例7: randomWiki
def randomWiki():
'''
This function gives you a list of n number of random articles
Choose any article.
'''
number=input("No: of Random Pages : ")
lst=wk.random(number)
for i in enumerate(lst):
print(i)
try:
key=input("Enter the number : ")
assert key>=0 and key<number
except AssertionError:
key=input("Please enter corresponding article number : ")
page=wk.page(lst[key])
url=page.url
#originalTitle=page.original_title
pageId=page.pageid
#references=page.references
title=page.title
#soup=BeautifulSoup(page.content,'lxml')
pageLength=input('''Wiki Page Type : 1.Full 2.Summary : ''')
if pageLength==1:
soup=fullPage(page)
print(soup)
else:
print(title)
print("Page Id = ",pageId)
print(page.summary)
print("Page Link = ",url)
#print "References : ",references
pass
示例8: get_random_wiki
def get_random_wiki():
random_wiki = wikipedia.random()
random_wiki_page = wikipedia.page(random_wiki)
summary = wikipedia.summary(random_wiki, sentences=1)
return random_wiki_page.title, random_wiki_page.url, summary
示例9: getArt
def getArt():
randArt = wikipedia.random(pages = 1)
userInput = input("Would you like to read about %s? " %randArt)
if userInput.lower() == "yes" or userInput.lower() == "y":
launchArt(randArt)
if userInput.lower() == "no" or userInput.lower() == "n":
getArt()
示例10: getRandArticle
def getRandArticle():
# getting directory for easy use
myPath = os.path.expanduser("~/documents/contexter/")
# how many articles to get
for x in xrange(0, 1):
# Get an article, if DisambiguationError, then try again
try:
article = wikipedia.random()
except wikipedia.exceptions.DisambiguationError:
print "Disambiguation, retrying..."
article = wikipedia.random()
except wikipedia.exceptions.PageError:
print "PageError, retrying..."
article = wikipedia.random()
pageObj = wikipedia.page(article)
text = pageObj.content
pageUrl = pageObj.url
# Writing the text and to the created file, encoded utf8
f = open(article + ".txt", "w")
f.write(text.encode('utf-8'))
f.close()
# Getting all lines
f = open(article + ".txt", "r")
tempText = f.readlines()
f.close()
# Decoding all lines for cleanup of file
for i, v in enumerate(tempText):
tempText[i] = tempText[i].decode('utf-8')
# Clean up the file, the file has '==' characters denoting sections
f = open(article + ".txt", "w")
for line in tempText:
if ('==' not in line):
f.write(line.encode('utf-8'))
f.close()
# move file into raw text folder
shutil.move(myPath + "scripts/" + article + ".txt", myPath + "/raw text/" + article + ".txt")
articleLoc = myPath + "/raw text/" + article + ".txt"
return [articleLoc, pageUrl]
示例11: fact
def fact():
pageTitle = wikipedia.random();
page = wikipedia.summary(pageTitle, sentences=1);
if ("is" in page or "was" in page) and page[0] != '<':
page = re.sub("[\(\[].*?[\)\]]", "", page)
return page
else:
return fact()
示例12: makePage
def makePage():
# get new random page
global page
text.delete(1.0, END)
try:
page = wikipedia.page((wikipedia.random(pages=1)))
except wikipedia.DisambiguationError:
makePage()
示例13: get_title
def get_title( progress_count ):
"""Obtain title of a random wikipedia article, checking it's not a disambiguation."""
# Print progress
sys.stdout.write( "%d " %progress_count )
sys.stdout.flush()
# Get title, try to load page. Pick another if there's a disambiguation error.
while True:
try:
title = wikipedia.random()
wikipedia.page( title )
break
except ( wikipedia.exceptions.DisambiguationError, wikipedia.exceptions.PageError ):
title = wikipedia.random()
return title
示例14: read_random_from_topic
def read_random_from_topic(topic = None):
"""Reads from a topic (or random topic if not specified), and returns
an internal content type that includes a random sentence"""
if not topic:
topic = wikipedia.random()
content = wikipedia.page(topic)
return to_system_format(content)
示例15: get_random_articles_v1
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))