def info(): query = "USA" #query = None query = query if query is not None else default_query print("Searching Wikipedia") results = wikipedia.search(query, results=5) if results is None: results = [wikipedia.search(default_query, results=3, suggestion=True)] print("Found searches: {}".format(results)) wiki_page = random.choice(results) wiki = wikipedia.page(wiki_page, auto_suggest=True, redirect=True) print("Loading wikipedia page: " + wiki_page) html = u"""<center><b>{}</b></center> <hr> <center>{}</center> <br> {}""" images = wiki.images random.shuffle(images) images_html = ["<img src={} width='200px'/>".format(image) for image in images[:10]] images_html = "".join(images_html) #html = html.format(wiki.title, wiki.html()) html = html.format(wiki.title, images_html, wiki.summary) return html
def wiki_search(option, opt_str, value, parser): """ used to search for a certain word. """ res = [] key_words = [] length = len(parser.rargs) if length < 1: parser.error(colored("option -S needs at least one argument!", "red", attrs=["bold"])) # there may be more than 2 arguments, for example: wiki -S wiki pedia 10 elif length >= 2: try: global num num = int(parser.rargs[-1]) except ValueError: parser.error("num should be a decimal number") exit(1) res = search(parser.rargs[0 : length - 1], results=num) key_words = parser.rargs[0 : length - 1] else: res = search(parser.rargs[0]) key_words = parser.rargs[0] w_print(res, "white", key_words, "green") return parser.rargs[0]
def predict_movie_filter(movie, year): """Predict movie title and return that title.""" my_searches = set(wikipedia.search('%s (%i film)' % (movie, year)) + wikipedia.search('%s (film)' % movie)) #list Comprehesions: [expression for item in list if conditional] searches = [element for element in list(my_searches) if (movie.lower() in element.lower()) & ('List of accolades' not in element)] #if list of searches (searches) is empty, return None, otherwise # convert everyhting to lower case if len(searches) == 0: return None else: searches = [search.lower() for search in searches] #return movie titles in preference of: # 1. movie (year film) # 2. movie (film) # 3. movie #otherwise: return searches[0] #note: must movie.lower because wikipedia package returns lower lmovie = movie.lower() first = '%s (%i film)' % (lmovie, year) second = '%s (film)' % lmovie if first in searches: return '%s (%i film)' % (movie, year) if second in searches: return '%s (film' % movie if lmovie in searches: return movie return searches[0]
def search(self, args): """::search:: used to search for a certain word. example: 1. search wikipedia -> return words refer to 'wikipedia' 2. search wiki pedia -> return words refer to 'wiki' & 'pedia' 3. search wiki pedia 10 -> return 10 of the results refer to 'wiki' & 'pedia' """ res, key_words = [], [] num = 0 if len(args) < 1: raise AssertionError(colored("function search needs at least one argument!", "red", attrs=["bold"])) # there may be more than 2 arguments, for example: search wiki pedia 10 elif len(args) >= 2: try: num = int(args[-1]) except ValueError: raise AssertionError("num should be a decimal number") res = wikipedia.search(args[0: len(args) - 1], results=num) key_words = args[0: len(args) - 1] else: res = wikipedia.search(args[0]) key_words = args[0] self.w_print(res, "white", key_words, "green")
def get_text(self): try: # do a wikipedia search for the topic topic_results = wikipedia.search(self.topic) # pick one of the results and grab the content self.content += wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content # DO IT MORE more_content = wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content if more_content not in self.content: self.content += more_content more_content = wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content if more_content not in self.content: self.content += more_content more_content = wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content except wikipedia.exceptions.DisambiguationError as e: self.content += self.topic + self.uncertain except wikipedia.exceptions.PageError as e: self.content += self.topic + self.unknown # if there are more than one word in the topic try to get some more results with the first and last word if len(self.topic.split()) > 1: try: # get more results with less of the topic for some ambiguity topic_results = wikipedia.search(self.topic.split()[:1]) self.content += wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content more_content = wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content if more_content not in self.content: self.content += more_content except wikipedia.exceptions.DisambiguationError as e: self.content += self.topic + self.uncertain except wikipedia.exceptions.PageError as e: self.content += self.topic + self.unknown try: # get even more with the second half of the topic for wierd results maybe topic_results = wikipedia.search(self.topic.split()[-1:]) self.content += wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content more_content = wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content if more_content not in self.content: self.content += more_content except wikipedia.exceptions.DisambiguationError as e: self.content += self.topic + self.uncertain except wikipedia.exceptions.PageError as e: self.content += self.topic + self.unknown try: # do a wikipedia search for the topic topic_results = wikipedia.search(self.topic[:len(self.topic) / 2]) # pick one of the results and grab the self.content self.content += wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content except wikipedia.exceptions.DisambiguationError as e: self.content += self.topic + self.uncertain except wikipedia.exceptions.PageError as e: self.content += self.topic + self.unknown return self.content.capitalize()
def get_text(self): try: # do a wikipedia search for the topic topic_results = wikipedia.search(self.topic) # pick one of the results and grab the content self.content += wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content # DO IT MORE more_content = wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content if more_content not in self.content: self.content += more_content more_content = wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content if more_content not in self.content: self.content += more_content more_content = wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content except wikipedia.exceptions.DisambiguationError as e: self.content += self.topic + ' can mean many things but to me it is' except wikipedia.exceptions.PageError as e: self.content += self.topic + ' is sometimes hard to find' # if there are more than one word in the topic try to get some more results with the first and last word if len(self.topic.split()) > 1: try: # get more results with less of the topic for some ambiguity topic_results = wikipedia.search(self.topic.split()[:1]) self.content += wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content more_content = wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content if more_content not in self.content: self.content += more_content except wikipedia.exceptions.DisambiguationError as e: self.content += self.topic + ' can mean many things but to me it is' except wikipedia.exceptions.PageError as e: self.content += self.topic + ' is sometimes hard to find' try: # get even more with the second half of the topic for wierd results maybe topic_results = wikipedia.search(self.topic.split()[-1:]) self.content += wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content more_content = wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content if more_content not in self.content: self.content += more_content except wikipedia.exceptions.DisambiguationError as e: self.content += self.topic + ' can mean many things but to me it is' except wikipedia.exceptions.PageError as e: self.content += self.topic + ' is sometimes hard to find' try: # do a wikipedia search for the topic topic_results = wikipedia.search(self.topic[:len(self.topic) / 2]) # pick one of the results and grab the self.content self.content += wikipedia.page(topic_results[rand(0, len(topic_results) - 1)]).content except wikipedia.exceptions.DisambiguationError as e: self.content += self.topic + ' can mean many things but to me it is' except wikipedia.exceptions.PageError as e: self.content += self.topic + ' is sometimes hard to find' return self.content
def search_page(searchterm): # Assume the user knows the specific page, it will be first result try: result = str(search(searchterm)[0]) except: # Transform unicode to ascii searchterm = convert_unicode(searchterm) result = search(searchterm)[0] return page(result)
def clean_leader(data): pope = wikipedia.search('current pope') data = data.replace('incumbent pope', pope[0]) data = re.sub(r'\[.+?\]\s?', '', data) data = re.sub(r'\(.+?\)\)s?', '', data).strip() if '{{' in data and '|' in data: # print data try: data = data.split('|')[1]+' and '+data.split('|')[2].replace('}}', '').strip() except IndexError: try: if '{{' in data and '|' in data: data = data.split('|')[1].replace('}}', '').strip() except IndexError: print data+'cacca22' pass if 'http' in data: data = data.split('http')[0].strip() if 'holds' in data: data = data.split('holds')[0].strip() # if 'current' in data: if 'current' in data and 'pope' not in data or 'unbulleted' in data: missing = wikipedia.search(data.replace('{{', '').replace('}}', '').replace('current', '').replace('unbulleted list', 'president of switzerland').replace('\n', '').strip()) try: missing = wikipedia.page(missing[0]) missing = missing.html() missing = BeautifulSoup(missing) for b in missing.find_all('b'): if 'incumbent' in b.get_text().lower(): missing_president = b.find('a').get_text() data = data.replace(data, missing_president) except IndexError: print data+'cacca3' pass except AttributeError: # print data+'cacca5' pass if '|' in data: data = data.split('|')[0].strip() if 'party' in data or 'congress' in data or 'front' in data or 'rally' in data: data = data.split('(')[0].strip() if ' of ' in data: data = data.split(' of ')[0].strip() data = re.sub(' ', ' ', data).replace('{{', '').replace('}}', '').strip().replace('(n)', '').replace(' ', ' ').replace('(independent ', '') if 'born' not in data: data = re.sub('([0-9])', '', data).replace(u"\u2013", '').replace('()', '').strip() return data
def getWiki(activity): pattern = re.compile("\\b(of|the|in|for|at|check|find|how|how\'s|is|tell|me|check|out|about|wiki|wikipedia|summarize)\\W", re.I) string = re.sub(pattern, "", activity) if "summarize" in activity or "summary" in activity: result = wikipedia.summary(string[len('summarize'):], sentences = 2) elif "wiki" in activity or "wikipedia" in activity: result = wikipedia.summary(activity[len('wiki'):]) else: try: result = wikipedia.search(string,results=10,suggestion=False) except Exception, e: result = wikipedia.search(string,results=10,suggestion=True)
def searchwiki(question): key = wikipedia.search(question) app.logger.info(repr("searching wikipedia for" + str(question))) if key: try: answer = wikipedia.summary(key[0],sentences=1) except Exception: m = wikipedia.page(wikipedia.search(key[0])) answer = wikipedia.summary(m.title,sentences=1) else: answer = False return answer
def search(self, search): """ Search string on wikipedia @param search as str @return [str] """ return wikipedia.search(search)
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: message.send('指定された言語 `{}` は存在しません'.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, }] message.send_webapi('', json.dumps(attachments)) else: message.send('`{}` に該当するページはありません'.format(query))
def disambiguationWikipedia(noun): """ Disambiguation for Wikipedia errors """ # Try to get wikipedia content try: wiki = wikipedia.page(noun) except wikipedia.exceptions.DisambiguationError as e: new = e.options[0] try: wiki = wikipedia.page(new) except: return 'Null' except wikipedia.exceptions.PageError: new = wikipedia.search(noun) try: wiki = wikipedia.page(new[0]) except: return 'Null' except: return 'Null' return wiki
def wiki(): ''' Search Anything in wikipedia ''' word=raw_input("Wikipedia Search : ") results=wk.search(word) for i in enumerate(results): print(i) try: key=input("Enter the number : ") except AssertionError: key=input("Please enter corresponding article number : ") page=wk.page(results[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
def wikipedia(query, gender_prefix): """**Endpoint** to make a **Wikipedia search** and return its **text content**. Args: query (str): Search query. gender_prefix (str): Prefix to address/call user when answering. Returns: JSON document. """ response = "" url = "" wikiresult = wikipedia_lib.search(query) if len(wikiresult) == 0: response = "Sorry, " + gender_prefix + ". But I couldn't find anything about " + query + " in Wikipedia." else: wikipage = wikipedia_lib.page(wikiresult[0]) wikicontent = "".join([ i if ord(i) < 128 else ' ' for i in wikipage.content ]) wikicontent = re.sub(r'\([^)]*\)', '', wikicontent) response = " ".join(sentence_segmenter(wikicontent)[:3]) url = wikipage.url data = {} data['response'] = response data['url'] = url return json.dumps(data, indent=4)
def get_wikipedia_content_based_on_word_count_train_validation(d_word_count): file = open('data/wikipedia_content_v1.txt', 'w') n_word = len(d_word_count.keys()) n_current = 0 for word in d_word_count.keys(): n_current += 1 print word, n_current, n_word, n_current * 1.0 / n_word if not word: continue lst_title = wiki.search(word) if len(lst_title) >= 1: for title in lst_title: title = title.encode('ascii', 'ignore') print 'title', title try: content = wiki.page(title).content.encode('ascii', 'ignore') except wiki.exceptions.DisambiguationError as e: print 'DisambiguationError', word ''' for title_disambiguation in e.options: title_disambiguation = title_disambiguation.encode('ascii', 'ignore') print 'title_disambiguation', title_disambiguation try: content = wiki.page(title_disambiguation).content.encode('ascii', 'ignore') except: pass ''' except: pass for line in content.split('\n'): line = ' '.join(map(util.norm_word, line.split())) if line: file.write(line + '\n') file.close()
def readTweet(): #takes on top 10 hashtags and @ tags fl = open("xiaomi.csv","r") x=object() tweet = '' lst = [] for line in fl: x=line.split(",") if len(x) > 3: tweet = x[-3] tweet = tweet.split(" ") for items in tweet: if items.startswith("#") or items.startswith("@"): print items lst = lst + [items] fdist=nltk.FreqDist(lst) print fdist g=nx.Graph() g.add_node("Xiaomi") g.add_nodes_from(fdist.keys()[:10]) nodes = [] for node in fdist.keys()[:10] : print node g.add_edge("Xiaomi",node) try : nodes = wikipedia.search(node)[:2] except Exception,e : print str(e) if len(nodes) >= 2 : g.add_nodes_from(nodes) g.add_edge(node,nodes[0]) g.add_edge(node,nodes[1])
def page(title=None, pageid=None, auto_suggest=True, redirect=True, preload=False): ''' Get a WikipediaPage object for the page with title `title` or the pageid `pageid` (mutually exclusive). Keyword arguments: * title - the title of the page to load * pageid - the numeric pageid of the page to load * auto_suggest - let Wikipedia find a valid page title for the query * redirect - allow redirection without raising RedirectError * preload - load content, summary, images, references, and links during initialization ''' if title is not None: if auto_suggest: results, suggestion = search(title, results=1, suggestion=True) try: title = suggestion or results[0] except IndexError: # if there is no suggestion or search results, the page doesn't # exist raise PageError(title) return WikipediaPage(title, redirect=redirect, preload=preload) elif pageid is not None: return WikipediaPage(pageid=pageid, preload=preload) else: raise ValueError("Either a title or a pageid must be specified")
def ne(self, ne): """ Returns Wikipedia article ids related to the given ne, downloading them if necessary """ ne = ne.lower() d = self.db.nes.find_one({'_id': ne}) if d is not None: return d # already processed # Not found -> download it try: search = wikipedia.search(ne) except wikipedia.exceptions.WikipediaException: search = [] pages = [] for title in search: found = False d = self.article(title=title) if d is None: continue pages.append(d['_id']) d = {'_id': ne, 'articles': pages} self.db.nes.insert_one(d) return d
def searchWiki(page): wikipedia.set_lang("fr") link = '' try: # p = wikipedia.page(page) # link = p.url propos = wikipedia.search(page,results=5,suggestion=False) for choice in propos: if choice.encode('utf-8') == page.encode('utf-8'): p = wikipedia.page(page) link = p.url break elif page in choice: #TODO print 'There is a proposition containing the keyWord ' print choice else: try: wikipedia.page(page,redirect=False,auto_suggest=False) except wikipedia.exceptions.RedirectError: p = wikipedia.page(page) link = p.url break except: link ='' except: link = "" return link#.encode('utf-8')
def hktry(txt): sdata=wiki.search(txt) for data in sdata: print data saytxt=wiki.page(sdata[0]).content[:500] saytxt=saytxt.encode('ascii','ignore') os.system('google_speech "'+saytxt+'"')
def wiki_func(paras, infos): """中文维基百科查询""" wikipedia.set_lang("zh") candidates = wikipedia.search(paras) if len(candidates) <= 0: return { 'text': 'not found', } else: summary = None for keyword in candidates: try: summary = wikipedia.summary(keyword, sentences=1) break except Exception: # 可能发生歧义异常,见 wikipedia 文档 continue if summary: answer = decode_to_unicode(summary) + \ u'\n候选关键词: %s' % u', '.join(candidates) return { 'text': answer, } else: return { 'text': 'not found', }
def download_page(self, title, depth): # skip if already visited if title in self.visited: return # skip if maximum depth reached if depth < 1: return # skip if max_pages reached if self.max_pages > 0 and self.page_counter >= self.max_pages: return # skip if title is undesirable if self.skip_title(title): return res = wp.search(title, results=1) if len(res) < 1: return try: page = wp.page(res[0]) except (wp.exceptions.DisambiguationError, wp.exceptions.PageError): return if page.title in self.visited: return # now actually adding the page to the collection self.visited.add(page.title) self.page_counter += 1 self.raw_pages[page.title] = page.content try: for link in page.links: self.download_page(link, depth - 1) except KeyError: return except Exception as e: logging.exception('Unexpected exception, please report: {}'.format(e))
def findWikipediaPage(film_title): possibles = (wikipedia.search(film_title) + wikipedia.search(film_title + ' (film)')) for m in possibles: a = re.match(r'(.*) \((\d+ )?film\)', m.lower()) if (a and (a.group(1).lower() in film_title.lower() or film_title.lower() in a.group(1).lower())): return m try: if (possibles[0].lower() in film_title.lower() or film_title.lower() in possibles[0].lower()): return possibles[0] else: return None except Exception as e: return None
def main(query, log_filename): """Search wikipedia articles on your terminal""" results = wikipedia.search(query, results=10) if not results: print 'No results found' return for idx, val in enumerate(results): print "%d. %s" % (idx + 1, val) print "\n-1 to exit" total_results = len(results) choice = 0 while not (1 <= choice <= total_results) and choice != -1: try: choice = int(raw_input("Choose from the above results: ")) except ValueError: continue if choice == -1: return page = wikipedia.page(results[choice - 1]) pipe = os.popen('less', 'w') pipe.write(codecs.encode(page.content, 'utf-8')) pipe.close() with open(log_filename, "a") as log_file: log_file.write(page.url)
def findRelevantArticles(term,data_path='.'): articleList = [] articles = wikipedia.search(term) #Setting suggestion = False (default value); No clear use for it now for article in articles: try: article = wikipedia.page(article) category_keywords = set(list(itertools.chain.from_iterable([category.lower().split() for category in article.categories]))) if len(category_keywords & relevant_categories) > 0: articlefilename = "content_"+str(article.title.lower())+".txt" if os.path.isfile(articlefilename): articlefilename = "content_"+ str(article.title.lower())+'%s.txt' % str(term+time.strftime("%Y%m%d-%H%M%S")) with codecs.open(os.path.join(data_path,articlefilename),'wb', 'utf-8') as outfile: content = wikipedia.page(article).content print>>outfile,content articleList.append(str(article.title)) except wikipedia.exceptions.PageError as e: pass except wikipedia.exceptions.DisambiguationError as e: for article in e.options: try: article = wikipedia.page(article) category_keywords = set(list(itertools.chain.from_iterable([category.lower().split() for category in article.categories]))) if len(category_keywords & relevant_categories) > 0: articlefilename = "content_"+str(article.title.lower())+".txt" if os.path.isfile(articlefilename): articlefilename = "content_"+ str(article.title.lower())+'%s.txt' % str(term+time.strftime("%Y%m%d-%H%M%S")) with codecs.open(os.path.join(data_path,articlefilename),'wb','utf-8') as outfile: print>>outfile,article.content articleList.append(str(article.title)) except wikipedia.exceptions.DisambiguationError as f: pass
def index(request): start = current_milli_time() print 'START: ', start auth = tweepy.OAuthHandler( settings.TWITTER_CONSUMER_KEY, settings.TWITTER_CONSUMER_SECRET ) auth.set_access_token( settings.TWITTER_ACCESS_TOKEN, settings.TWITTER_ACCESS_SECRET_TOKEN ) twitter = tweepy.API(auth) if 'query' in request.GET: query = request.GET['query'] twitterdata = twitter.search(query, 'en') print 'AFTER TWITTER CALL: ', current_milli_time() - start wikidata = wikipedia.search(query) print 'AFTER WIKIPEDIA CALL: ', current_milli_time() - start else: twitterdata = None wikidata = None return render(request, 'index.html', {'twitter': twitterdata, 'wikipedia': wikidata })
def wikiwatch(): """ Search wikipedia """ try: if text.lower().find("!wiki") != -1 or text.lower().find("!wikipedia") != -1 or text.lower().find("!w ") != -1: string = text[text.lower().rfind("!w") :] search = string[string.find(" ") + 1 :].rstrip("\n") search_list = text.split()[4:] # search = " ".join(search_list) summary = wikipedia.summary(search, sentences=2) if len(summary) > 430: summary = summary[0:430] + "..." # print(summary) page = wikipedia.page(search) title = page.title url = page.url sendmsg(title + " | " + url) try: sendmsg(summary.encode("utf-8", "ignore")) except: sendmsg("fix this when you have the time, probably to do with non-unicode characters.") except: try: search_list = text.split()[4:] search = " ".join(search_list) lookup = wikipedia.search(search, results=5) # page = wikipedia.page(lookup[0]) # title = page.title url = "https://en.wikipedia.org/wiki/" + lookup[0].replace(" ", "+") sendmsg("I'm not sure what you mean, this is what I could find. | " + url) except: sendmsg(error)
def get_info_box_data(word): wikipedia.set_lang("He") res = wikipedia.search(word, 10, True) title = res[0][0] html_page = wikipedia.page(title).html() soup = BeautifulSoup(html_page) info_table = soup.find("table", {"class": "infobox"}) info = [] current_tuple = tuple() rows = info_table.findChildren(["th", "tr", "td"]) for row in rows: result = "" row_title = get_title(row) values = row.findChildren("a") if len(values) == 0: continue for value in values: # value = cell['content'] # print "The value in this cell is %s" % value for content in value.contents: if "img" in content: continue result += " " + (str)(content) if "img" in result: continue
def crawlAndLearn(topic): print 'topic: ' + str(topic) if not 'educatedOn' in meta: meta['educatedOn'] = [] if topic in meta['educatedOn']: print "Already Learned: " + topic else: search = wikipedia.search(topic, results=int(config['readMoreLimit'])) for page in search: print "Learning about: " + page try: article = wikipedia.page(page) content = re.sub(r'=+\sSee also\s=+.+$', ' ', article.content, flags=re.M | re.S) content = re.sub(r'=+\s.+\s=+', ' ', content) content = re.sub(r'\(.+\)', ' ', content, flags=re.M | re.S) #print content iterateInput(content) except wikipedia.exceptions.DisambiguationError: content = "" except wikipedia.exceptions.PageError: content = "" if not 'educatedOn' in meta: meta['educatedOn'] = [] meta['educatedOn'].append(topic)
def search(self, topic): terms = wikipedia.search(topic)[:5] message = '<Wikipedia topics>\n' message += '\n'.join(terms) return message
def search_db(keywords, response_url): # Remove stopwords, special characters, emojis, lowercase the keywords, # spelling correction, etc keywords = emoji_pattern.sub(r'', keywords) # print(1, keywords) keywords = keywords.strip().split() query_keywords = keywords query_keywords = [word for word in query_keywords if word[0] != '(' and word [-1] != ')'] query_keywords = [str(re.sub(all_configurations.SPECIAL_CHARACTERS, ' ', word).lower()) for word in query_keywords if word not in stop_words] query_keywords = (" ".join(query_keywords)).strip() # print(2, keywords) keywords = [str(re.sub(all_configurations.SPECIAL_CHARACTERS, ' ', word).lower()) for word in keywords if word not in stop_words] # print(3, keywords) keywords = (" ".join(keywords)).strip() # print(keywords) connection = pg.connect( host=all_configurations.HOST, user=all_configurations.USER, dbname=all_configurations.DATABASE, password=all_configurations.DB_PWD, port=all_configurations.PORT ) connection.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) cursor = connection.cursor() query = """SELECT {} FROM {} WHERE LOWER({}) LIKE '% {}%';""".format( 'link', 'forum_data', 'topic', query_keywords ) cursor.execute(query) forum_result = cursor.fetchall() # query = """SELECT {} FROM {} WHERE LOWER({}) = '{}';""".format( # 'content', # 'ipterms', # 'term', # keywords # ) # cursor.execute(query) # ipterm_result = cursor.fetchall() final_result = "" # if ipterm_result: # final_result += ipterm_result[0][0] # else: # print('{} not found in ipterms.'.format(keywords)) # Adding a bogus comment to make changes try: final_result += wiki.summary(keywords) except wiki.DisambiguationError: text = "` Multiple results found. Choose a specific term from the ones given below. " \ "(Please use the exact keywords to specify your query) :` \n\n" text += "\n".join(wiki.search(keywords)) payload = { 'text': text, 'user': '******' } requests.post(response_url, data=json.dumps(payload)) return except wiki.PageError: split_words = keywords.split(" ") corrected_words = [str(tb(word.strip()).correct()) for word in split_words] keywords = " ".join(corrected_words).strip() try: final_result += wiki.summary(keywords) except wiki.DisambiguationError: text = "` Multiple results found. Choose a specific term from the ones given below. " \ "(Please use the exact keywords to specify your query) :` \n\n" text += "\n".join(wiki.search(keywords)) payload = { 'text': text, 'user': '******' } requests.post(response_url, data=json.dumps(payload)) return except wiki.PageError: pass payload = { 'text': "Please ensure you've used the correct spelling and/or keywords.", 'user': '******' } requests.post(response_url, data=json.dumps(payload)) return except Exception as e: print("Wiki exception occurred: ", e) except Exception as e: print("Wiki exception occurred: ", e) if forum_result: final_result += "\n\n\n ` Here are a few forum discussions related to the topic " \ "that may be useful: ` \n" num = 0 for res in forum_result: num += 1 if num > 10: final_result += "\n\n*Found {} related forum posts. To have the full list please use the _'{}'_ slash command.*".format( len(forum_result), all_configurations.FORUM_POST_SLASH_COMMAND ) break final_result += " > " + res[0] + "\n" elif not final_result: final_result = "No results found. Please ensure you've used the correct spelling and/or keywords. " \ "\nOr try using more specific terms in your query." else: final_result += "\n\n\n ` NO RELATED FORUM POST FOUND. `" cursor.close() connection.close() # print(final_result) payload = { 'text': final_result, 'user': '******' } requests.post(response_url, data=json.dumps(payload))
апи и рассмотрели, как встроить Википедию в Пайтон. ''' #Подключаем модуль Wikipedia API - его мы скачали через pip import wikipedia #И сразу выведем короткую инфу по запросу "Wikipedia" print wikipedia.summary("Wikipedia") ''' В консоли будет такое: Wikipedia (/ˌwɪkɨˈpiːdiə/ or /ˌwɪkiˈpiːdiə/ WIK-i-PEE-dee-ə) is a collaboratively edited, multilingual, free Internet encyclopedia supported by the non-profit Wikimedia Foundation... ''' #Не знаете, что конкретно нужно? Сделаем поиск по ключевому слову! wikipedia.search("Barack") ''' Вот такой список результатов получим: [ u'Barak (given name)', u'Barack Obama', u'Barack (brandy)', u'Presidency of Barack Obama', u'Family of Barack Obama', u'First inauguration of Barack Obama', u'Barack Obama presidential campaign, 2008', u'Barack Obama, Sr.', u'Barack Obama citizenship conspiracy theories', u'Presidential transition of Barack Obama' ] '''
from wordcloud import WordCloud, STOPWORDS import matplotlib.pyplot as plt import wikipedia # Search the keyword in wikipedia wiki = wikipedia.search('artificial inteligence') # Specify the title of the Wikipedia page wiki = wikipedia.page(wiki[0]) # Extract the plain text content of the page text = wiki.content stopwords = set(STOPWORDS) wordcloud = WordCloud(background_color='white', colormap='winter', stopwords=stopwords).generate(text) plt.figure(figsize=(10, 10)) plt.imshow(wordcloud) plt.axis("off") plt.show()
def pesquisasigla(sigla1): print("Pesquisando...") print(sigla1) sigla = sigla1.upper() s = wikipedia.search(sigla) #LISTA DE PESQUISA cont = 0 for explicacao in s: #PARA CADA SIGNIFICADO print('sumario :' + str(cont + 1)) if sigla1 in pesquisadas: #palavra ja foi identificada como sigla return 1 if sigla1 in pesquisadasnao: #palavra ja foi identificada como nao sigla return 0 if cont == 1: #numero maximo de sumarios pesquisadasnao.append(sigla1) return 0 try: try: sumario = wikipedia.summary(explicacao, sentences=1) #BUSCO O SUMARIO except (wikipedia.exceptions.PageError, wikipedia.exceptions.DisambiguationError) as g: try: sumario = wikipedia.summary(s[cont], sentences=1) except wikipedia.exceptions.DisambiguationError as j: #so nomes causam esse erro return 0 except wikipedia.exceptions.DisambiguationError as e: try: if len(e.options) > 0: sumario = wikipedia.summary( e.options[0], sentences=1) #SE TIVER MAIS DE UM, PEGO O PRIMEIRO except wikipedia.exceptions.DisambiguationError as f: if len(e.options) > 0: sumario = wikipedia.summary(e.options[1], sentences=1) if ' ' in sumario: #verifica se os sumario contém espaços sumario = sumario.split(" ") #divide o sumario em palavras for i, palavra in enumerate(sumario): #objeto entre parentesês--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- if palavra == '(' + sigla + ')' or palavra == '(' + sigla + ').' or palavra == '(' + sigla + '),' or palavra == " (" + sigla + ") " or palavra == ' (' + sigla + '),' or palavra == '(' + sigla1 + ')' or palavra == '(' + sigla or palavra == '(' + sigla + 's)': pesquisadas.append( sigla1) #coloca na lista de pesquisadas sendo sigla return 1 #------------------------------------------------------------------------------------------------------------------------------------------------------------------------ if (palavra == sigla + ')' or palavra == sigla + '),' or palavra == sigla + ').'): pesquisadas.append( sigla1) #coloca na lista de pesquisadas sendo sigla return 1 #Caso 2-Explicação padrao--------------------------------------------------------------------------------------------------------------------------------------------- if (palavra == sigla or palavra == sigla1): if (sumario[i + 1] != ''): if sumario[i + 1][0] == '(' and sumario[ i + 1] == "(sigla" or sumario[ i + 1] == "(anteriormente": pesquisadas.append( sigla1 ) #coloca na lista de pesquisadas sendo sigla return 1 else: if sumario[i + 2][0] == '(' and sumario[ i + 2] == "(sigla" or sumario[ i + 2] == "(anteriormente": pesquisadas.append(sigla1) return 1 #------------------------------------------------------------------------------------------------------------------------------------------------------------------------- cont = cont + 1 return 0
def on_chat_message(msg): content_type, chat_type, chat_id = telepot.glance(msg) print('Chat:', content_type, chat_type, chat_id) command = msg['text'].lower() if command == '/start': markup = InlineKeyboardMarkup(inline_keyboard=[[ dict(text='☂️Github', url='https://t.me/abdisamerga/'), dict(text='Switch inline', switch_inline_query='initial query') ], []]) responce = bot.getChat(chat_id) first_name = responce['first_name'] bot.sendMessage( chat_id, f'Hello <b>{first_name}! </b> Botisfy is a web-based tool that enables users to search information on the World Wide Web. <b>search specific phrase</b> like <code>cat</code>', parse_mode='html', reply_markup=markup) if command != '/start': vd = googlesearch.search(f'{command}', lang='en') try: photo_url_from_google = vd[0] except: pass try: wikipedia.set_lang("en") var = wikipedia.search(f'{command}') try: value = var[0] except IndexError: bot.sendMessage( chat_id, f'opps! Your search-<b>{command}</b> did not match any documents.', parse_mode='html', reply_markup=None) title = str(value) try: lan = wikipedia.summary(f'{title}', sentences=3, auto_suggest=False, redirect=False) except: lan = wikipedia.summary(f'{title}', sentences=3, auto_suggest=True, redirect=True) bot.sendPhoto(chat_id, photo=f'{str(photo_url_from_google)}', caption=f'{lan}', reply_markup=None) except Exception: wikipedia.set_lang("en") var2 = wikipedia.search(f'{command}') global value2 try: value2 = var2[0] except (IndexError, UnboundLocalError, NameError): pass title2 = str(value2) try: lan2 = wikipedia.summary(f'{title2}', sentences=3, auto_suggest=False, redirect=False) except: lan2 = wikipedia.summary(f'{title2}', sentences=3, auto_suggest=True, redirect=True) try: photo_url_from_wiki = wikipedia.page(title2).url bot.sendPhoto(chat_id, photo=f'{str(photo_url_from_wiki)}', caption=f'{lan2}', reply_markup=None) except (wikipedia.exceptions.PageError, wikipedia.exceptions.DisambiguationError, wikipedia.DisambiguationError): bot.sendMessage(chat_id, f'{lan2}', reply_markup=None)
import sys currdir = os.path.dirname(__file__) if __name__ == '__main__': form = sg.FlexForm('GUI for the Project') # begin with a blank form layout = [[ sg.Text( 'Type in the topic of wikipedia, you want to get WordCloud for') ], [sg.Text('Enter Topic', size=(15, 1)), sg.InputText('')], [sg.Submit(), sg.Cancel()]] button, values = form.Layout(layout).Read() title = wikipedia.search(str(values))[0] page = wikipedia.page(title) text = page.content mask = np.array(Image.open(os.path.join(currdir, "cloud.png"))) stop = set(STOPWORDS) wc = WordCloud(background_color="white", mask=mask, max_words=200, stopwords=stop) messages = nltk.sent_tokenize(text) corpus = [] lemmatizer = WordNetLemmatizer() stemmer = PorterStemmer() for i in range(len(messages)): temporary = re.sub('[^a-zA-Z]', ' ', messages[i])
def wiki(fulltext): tokens = fulltext.split(' ') response = '' global page_summary_splits global counter if tokens[0].lower() == 'search': print('got a search') search_term = fulltext[7:] search_result = wikipedia.search(search_term) search5 = search_result[:5] [response + ', ' + result for result in search5][2:] response = ", ".join(search5) response = response[:150] if response == '': response = "No entries found!" ###print(search_term, response) elif tokens[0].lower() == 'wiki': print('got a wiki') counter = 1 page_title = sub(' +', ' ', fulltext[4:].rstrip().lstrip()) try: page_summary_full = wikipedia.summary(page_title) if page_summary_full == '': response = "This page does not exist, try using english by replying: english, or try using 'search' and replying with correct spelling..." return response nchars = 300 page_summary_splits = [ page_summary_full[i:i + nchars] for i in range(0, len(page_summary_full), nchars) ] response = 'PART 1 of {}: '.format( str(len(page_summary_splits) )) + page_summary_splits[0] + '... [reply: "more"]' except wikipedia.PageError: response = "This page does not exist, try using 'search' and replying with correct spelling..." ###print(page_title, response) elif tokens[0].lower().rstrip().lstrip() == 'more': if counter >= len(page_summary_splits): response = "END OF SUMMARY..." else: response = 'PART {} of {}: '.format( str(counter + 1), str(len(page_summary_splits) )) + page_summary_splits[counter] + '...' counter += 1 elif tokens[0].lower() == 'about': response = "Welcome to Wikipedia SMS, created by TUNIO 2019" elif tokens[0].lower().rstrip().lstrip() == 'how': response = "to set language to urdu, reply: urdu, to set language to english reply: english, to search reply with: search Albert Einstein, to open a page reply with: wiki Albert Einstein, to look up a word in dictionary reply with: define abstraction" elif tokens[0].lower().rstrip().lstrip() == 'urdu': wikipedia.set_lang('ur') elif tokens[0].lower().rstrip().lstrip() == 'english': wikipedia.set_lang('en') elif tokens[0].lower().rstrip().lstrip() == 'define': word = sub(' +', ' ', fulltext[6:].rstrip().lstrip()) dictionary_lookup = dictionary.meaning(word) pretty_str = pprint.pformat(dictionary_lookup) response = pretty_str else: print('got a poor format: ', fulltext) response = "poor formatting!, reply with: how" return response
if (len(j) > 2): model_mat = model_data['mat'] threshold = model_data['thresh'] boz = gib_detect_train.avg_transition_prob(j, model_mat) > threshold if (boz == True): j = " ".join(j.split()) degree.append(j) d = dog("bolt://localhost:7687", "neo4j", "mathers22") degree = list(dict.fromkeys(degree)) print(degree) subject_er = 'Electromagnetics' subject_e = wikipedia.search(subject_er)[0] print(subject_e) # d.add_person(subject_e,degree) while ("" in degree): degree.remove("") lister = [] for i in range(len(degree)): try: j = wikipedia.search(degree[i])[0] res = j.replace("'", " ") lister.append(res)
def get_wiki(query): title = wp.search(query, results=1) page = wp.page(title) return page.content
wordList = nameQuestion.split(" ") for word in wordList: if word.lower() not in stopwords: print(random_greeting + word) name = word feelingQuestion = raw_input('How are you feeling today? ') feelingWord = feelingQuestion.split(" ") for word in feelingWord: if word.lower() == "not": flip = True elif word in happyWordsfile: emotion = "good" elif word in sadWordsfile: emotion = "bad" if flip and emotion == "good": print("Oh dear, sound like you need a beer!") if not flip and emotion == "good": print("Thats really good " + name) if flip and emotion == "bad": print("Thats really good " + name) if not flip and emotion == "bad": print("Oh dear, sound like you need a beer!") yourColour = raw_input("What is your hair colour ?") print(random_hair) while True: search = raw_input("Want to find some information about any subject>>") search_words = search.split(" ") for word in search_words: if (word not in stopwords) and (len(word.strip()) > 0): print("Just about to search for " + word) results = wikipedia.search(word) print(wikipedia.summary(results[0], sentences=1))
language = 'en' # en - English, fr - French #Initializations choice = -1 choice1 = -1 search = [] # Until the user wants to quit intro = "What do you wanna search?" while (choice != 0): recordChoiceAudio(intro) searchK = input(intro) # Try to get relevant searchs limited to 2 search = wikipedia.search(searchK, results=2) print(search) try: response = requests.get(wikipedia.page(search[0]).url) searchKey = search[0] except: response = requests.get(wikipedia.page(search[1]).url) searchKey = search[1] recordChoiceAudio( "Say 1 to listen to the summary or 2 to listen the titles") soup = BeautifulSoup(response.content, "html.parser") titleList = [item.get_text() for item in soup.select("h2 .mw-headline")] time.sleep(1) titleList1 = deepcopy(titleList[:-4])
import wikipedia as wiki import re import pandas as pd data = pd.read_csv("names.csv", names=['num', 'name']) names = list(data['name']) yils = [] for n in names: try: p = wiki.page(wiki.search('nba ' + n)[0]) print('-', end='') sum = p.summary print('-', end='') draft_year = int(re.findall(r'([0-9]+)+ (NBA)? draft', sum)[0][0]) print('- ', end='') yil = 2015 - draft_year print("{} - {} - {:2}".format(n, draft_year, yil)) yils.append(yil) except: print("Fail at {}".format(n)) yils.append(-1) data['yil'] = yils data.to_csv(r'./temp.csv')
url = url[4:] if url[-1:] == '/': url = url[:-1] return url.lower() file = open( inputFile) # Opens 'inputFile' and appends entries to hostnames[] array reader = csv.reader(file) for item in reader: hostnames.append(urlStrip(item[0])) file.close() for item in hostnames: # Loop will check if hostnames have wikipages if item not in urlFound + urlNotFound: # Prevents checking duplicate hostnames s = wikipedia.search(item, results=1) # Searches Wikipedia for hostname urlBool = False # T/F boolean for checking if hostname has wikipage infoboxUrl = [] # Contains hrefs from wikipage infobox counter += 1 if s: # If Wikipedia page was found, open with BeautifulSoup site = urllib.parse.quote('http://en.wikipedia.org/wiki/' + s[0], safe='/,:') page = urlopen(Request(site, headers={'User-Agent': 'Mozilla/5.0'})) soup = BeautifulSoup(page.read(), 'lxml') table = soup.find('table', class_='infobox vcard') if not table: # Finds infobox table on wikipage table = soup.find('table', class_='infobox') if table: # Search each row for 'Website' cell and append hrefs
def get_ans(user_ques): # Append user question to the log file logfile.write('%s\n\n' %user_ques) #create a regular expression pattern to seggregate the user question re_1 = re.compile(r'('+"|".join(wh_ques)+') ('+"|".join(helping_verbs)+') (.*)\?', re.IGNORECASE) #check if regular expression matches the user question m = re_1.match(user_ques) if(m is None): return #If match is found assign the the words as wh, hv and object wh = m.group(1) hv = m.group(2) obj = m.group(3) #If the question starts with is Where, Who and What if(wh.lower() != "when"): # tokenize the obj to extract noun from it tagged_data = nltk.pos_tag(nltk.word_tokenize(obj)) #Append POS tagged string to the log file logfile.write(str(tagged_data)) # Extract the noun and verb present in the sentence noun = " ".join([word_tag[0] for word_tag in tagged_data if word_tag[1]=="NNP"]) verbs = [word_tag[0] for word_tag in tagged_data if word_tag[1]=="VBD"] if(len(verbs) == 1): verb = verbs[0] #Checking for synonyms of the verb present in the question from wordnet syns = [l.name() for syn in wordnet.synsets(verb) for l in syn.lemmas()] else: verb = "" #If verb is present in the question if(verb != ""): #Search for the noun in wikipedia search_res = wikipedia.search(noun) #Append wikipedia search results to logfile logfile.write(str('%s\n\n' %search_res)) else: #search for object in wikipedia search_res = wikipedia.search(obj) #Append wikipedia search results to logfile logfile.write(str('%s\n\n' %search_res)) #Store the results in a array called findings findings = [] for res in search_res: #Extract the content present in the first link available on wikipedia content = wikipedia.page(res).content #Sentence tokenize the data sentences = tokenizer.tokenize(content) if(verb != ""): for sent in sentences: # search for the verb present in the question and its synonyms among the tokenized sentences if(re.search('('+verb+'|'+"|".join(syns)+')', sent, re.IGNORECASE) is not None): #search for noun present in the question among the results obtained from above search if(re.search(noun, sent, re.IGNORECASE) is not None): findings.append(sent) else: findings.append(sent) #Append word search results to logfile logfile.write(str('%s\n\n' %findings)) #If there are no findings display the no answer found sentence if(len(findings) == 0): return "I am sorry, I do not know the answer." #Print the answer in the format of noun followed by helping verb, followed by our result from find else: #Store the first available result in find variable find = findings[0] #Append final sentence extracted to logfile logfile.write(str('%s\n\n' %find)) #Print the result return noun+" "+hv+" "+ find[find.index(verb):] else: for sent in sentences: #search for noun among tokenized sentences if(re.search(noun, sent, re.IGNORECASE) is not None): #search for helping verb among results obtained from above if(re.search(" "+hv+" ", sent, re.IGNORECASE) is not None): findings.append(sent) #Append word search results to logfile logfile.write(str('%s\n\n' %findings)) if(len(findings) == 0): return "I am sorry, I do not know the answer." #store results in findings find = findings[0] #Append final sentence extracted to logfile logfile.write(str('%s\n\n' %find)) #Print the result return obj+" "+ find[find.index(hv+" "):] else: #When case #Split the Object into name and y arr = obj.split() name = " ".join(arr[:-1]) y = arr[-1] #Search for synonyms of y syns = [l.name() for syn in wordnet.synsets(y) for l in syn.lemmas()] #Search for name in wikipedia search_res = wikipedia.search(name) #Append wikipedia search results to logfile logfile.write(str('%s\n' %search_res)) findings = [] for res in search_res: #Extract the results from first link available on wikipedia content = wikipedia.page(res).content #Setence tokenize the data sentences = tokenizer.tokenize(content) for sent in sentences: #search for name if(re.search(name, sent, re.IGNORECASE) is not None): #search for y and its synonms if(re.search('('+y+'|'+"|".join(syns)+')', sent, re.IGNORECASE) is not None): if(re.search(" on ", sent, re.IGNORECASE) is not None): findings.append(sent) #Append word search results to logfile logfile.write(str('%s\n\n' %findings)) if(len(findings) == 0): return "I am sorry, I do not know the answer." find = findings[0] #Append final sentence extracted to logfile logfile.write(str('%s\n\n' %find)) #Search for the date in the string sobj = re.search(r'\d{4}', find, re.IGNORECASE) year = sobj.group() #Print the results return name+" "+hv+" "+y+" "+find[find.index("on"):find.index(year)+4]
def page(self, term): return wikipedia.search(term, results=1)
def wiki_search(): page_or_phrase = input('Type page name or phrase here:') while page_or_phrase != '': page_or_phrase = input('Type page name or phrase here:') search_page = wikipedia.search(page_or_phrase) print(wikipedia.summary(search_page))
import urllib2 as url import wikipedia as w from bs4 import BeautifulSoup as bs import re # A dictionary to store the data we'll retrieve. d = {} # 1. Grab the list from wikipedia. w.set_lang('pt') s = w.page(w.search('Lista de Senadores do Brasil da 55 legislatura')[0]) html = url.urlopen(s.url).read() soup = bs(html, 'html.parser') # 2. Names and links are on the second column of the second table. table2 = soup.findAll('table')[1] for row in table2.findAll('tr'): for colnum, col in enumerate(row.find_all('td')): if (colnum + 1) % 5 == 2: a = col.find('a') link = 'https://pt.wikipedia.org' + a.get('href') d[a.get('title')] = {} d[a.get('title')]['link'] = link # 3. Now that we have the links, we can iterate through them, # and grab the info from the table. for senator, data in d.iteritems(): page = bs(url.urlopen(data['link']).read(), 'html.parser') # (flatten list trick: [a for b in nested for a in b]) rows = [ item for table in
import wikipedia S = wikipedia.search('Ham and Cheese Sandwich') page = wikipedia.WikipediaPage(S[0]) i = 0 while i < 20: page = wikipedia.WikipediaPage(page.links[int(random.random() * len(page.links))]) print(page.title) i += 1
def display_search(term): try: print("Search Terms: " + str(wikipedia.search(term))) except wikipedia.exceptions.DisambiguationError as e: print("Ambiguous term, select one of the terms below:") print(e.options)
def search_articles(key_phrase): return wikipedia.search(key_phrase)
import wikipedia import pandas as pd with open('keywords.txt', 'r') as f: keywords = f.readlines() keywords = [word.lower().strip('\n') for word in keywords] words = [] articles = [] for word in keywords: try: search = wikipedia.search(word) article = wikipedia.page(search[0]).content.lower() words.append(word) articles.append(article) except Exception as e: continue df = pd.DataFrame(list(zip(words, articles)), columns=['name', 'Document']) df.to_csv('articles.csv', index=False)
def bot_reply(text): # text = input() response = "" if text == None: return f"{get_emoji()}" if text != None: for i in text: if i in emoji.UNICODE_EMOJI: response = response + f"{get_emoji()}" else: response = response + f"{get_emoji()}" if text != None: for i in text.split(): if i.lower() in [ "bye", "leave", "exit", "quit", 'bye', 'see you', 'goodbye', 'good bye', 'exit', 'leave', 'go', 'tata', 'see ya' ]: return end_conv(i) response = response + greeting(text) if "who are you" in text.lower() or "name yourself" == text.lower( ) or "tell your name" == text.lower() or "" in text.lower( ) == "whats you name" in text.lower() or "what's your name" == text.lower( ) or "what is your name" == text.lower() or "what your name" == text.lower( ): response += random.choice([ "I am buddy, don't like teddy, but i still love candy. HA HA HA!!!", "Buddy Here. ", "Buddy your friend. " ]) if "who is your botmaster" in text.lower( ) or "name of your botmaster" in text.lower( ) or "name your botmaster" in text.lower( ) or "who is your master" in text.lower( ) or "name of your master" in text.lower( ) or "name your master" in text.lower(): return random.choice( ["Udit is the one who made me ... ", "U.D.I.T <- He is the one."]) if "tell me about your botmaster" in text.lower( ) or "tell about botmaster" in text.lower( ) or "about botmaster" in text.lower( ) or "tell me about your master" in text.lower( ) or "tell about master" in text.lower() or "about master" in text.lower(): response += "Well, he is a programmer. To know more about him check this out https://github.com/uditkumar01" if 'date' in text.lower(): get_date = getDate() response = response + ' ' + get_date if 'what is the time' in text.lower() or 'Tell me the time' in text.lower( ) or 'Tell me time' in text.lower( ) or 'what\'s the time' in text.lower() or 'whats the time' in text.lower( ) or 'check the time' in text.lower() or 'check the clock' in text.lower( ) or 'check time' in text.lower() or 'check clock' in text.lower(): get_time = getTime() response = response + ' ' + get_time if 'who is' in text.lower(): # print(text) words = text.split() person = " ".join(words[2:]) # person = wikipedia.suggest(person) print("person", person) my_results = wikipedia.search(text) all_results = [] for i in my_results: if person.lower() == i.lower(): return response + ' ' + search_wiki(person) if "bill" in i.lower(): all_results.append(i) print("check1", all_results) if len(all_results) > 1: return f"Which {person} you are talking about: {' ,'.join(all_results)}" # response = person response = response + ' ' + search_wiki(person) if 'what is' in text.lower(): words = text.split() person = " ".join(words[2:]) # response = person response = response + ' ' + search_wiki(person) if "i" not in text.lower() or "name" not in text.lower(): text = spell_check(text.split()) if response == "": response = replying(text) return response
# except: # queue_full = False # #create as many threads as you want # thread_count = 5 # thread_list = [] # for i in range(thread_count): # t = threading.Thread(target=worker, args = (q,)) # thread_list.append(t) # t.start() # for thread in thread_list: # t.join() import wikipedia import threading querylist = wikipedia.search("Federer")[0:5] output = [] def worker(query): text = wikipedia.page(query).content print(text) output.append(text.split()[0:3]) thread_list = [] for i in range(5): t = threading.Thread(target=worker, args=(querylist[i],)) t.start() thread_list.append(t) for thread in thread_list: thread.join() print(output)
import wikipedia # print the summary of what python is print(wikipedia.summary(req_name)) #wikipedia.summary(req_name, sentences=2) #fixed number of sentences in wiki summary #https://stackoverflow.com/questions/15228054/how-to-count-the-amount-of-sentences-in-a-paragraph-in-python import re par=wikipedia.summary(req_name) text=re.split(r'[.!?]+', par) #text contains list of lines in wikipedia search query for request name print_list(text) len(text) result = wikipedia.search(req_name) print_list(result) #similar results as req_name page = wikipedia.page(result[0]) title = page.title print(page) print() print(title) print() categories = page.categories print_list(categories) # get the whole wikipedia page text (content) content = page.content
def my_wiki(message): """ Обрабатывает запрос и пересылает результат. Если запроса нет, выдаёт рандомный факт. :param message: :return: """ # обрабатываем всё, что пользователь ввёл после '/wiki ' if not len(message.text.split()) == 1: your_query = ' '.join(message.text.split()[1:]) user_action_log( message, "entered this query for /wiki:\n{0}".format(your_query)) try: # определяем язык запроса. Эвристика для английского и русского if all(ord(x) < 127 or not x.isalpha() for x in your_query): wikipedia.set_lang('en') # TODO: a bit dirty condition elif all( ord(x) < 127 or ( ord('Ё') <= ord(x) <= ord('ё')) or not x.isalpha() for x in your_query): wikipedia.set_lang('ru') else: wikipedia.set_lang(detect(your_query)) wiki_response = wikipedia.summary(your_query, sentences=7) if '\n \n' in str(wiki_response): wiki_response = "{}...\n\n" \ "<i>В данной статье " \ "имеется математическая вёрстка. " \ "Пожалуйста, перейди по ссылке:</i>".format(str(wiki_response).split('\n \n', 1)[0]) # print(wiki_response) # извлекаем ссылку на саму статью wiki_url = wikipedia.page(your_query).url # извлекаем название статьи wiki_title = wikipedia.page(your_query).title my_bot.reply_to(message, "<b>{0}.</b>\n{1}\n\n{2}".format( wiki_title, wiki_response, wiki_url), parse_mode="HTML") user_action_log( message, "got Wikipedia article\n{0}".format(str(wiki_title))) # всё плохо, ничего не нашли except wikipedia.exceptions.PageError: my_bot.reply_to(message, "Запрос не найден.") user_action_log(message, "didn't received any data.") # нашли несколько статей, предлагаем пользователю список except wikipedia.exceptions.DisambiguationError as ex: wiki_options = ex.options my_bot.reply_to( message, "Пожалуйста, уточни запрос. " "Выбери, что из перечисленного имелось в виду, " "и вызови /wiki ещё раз.\n" + "\n".join(map(str, wiki_options))) print("There are multiple possible pages for that article.\n") # берём рандомную статью на рандомном языке (языки в config.py) else: wikipedia.set_lang(random.choice(['en', 'ru'])) wikp = wikipedia.random(pages=1) try: print("Trying to get Wikipedia article\n{0}".format(str(wikp))) wikpd = wikipedia.page(wikp) wiki_fact = wikipedia.summary(wikp, sentences=3) my_bot.reply_to(message, "<b>{0}.</b>\n{1}".format(wikpd.title, wiki_fact), parse_mode="HTML") user_action_log(message, "got Wikipedia article\n{0}".format(str(wikp))) except wikipedia.exceptions.DisambiguationError: wikp = wikipedia.random(pages=1) try: print("Trying to get Wikipedia article\n{0}".format(str(wikp))) wiki_var = wikipedia.search(wikp, results=1) print("There are multiple possible pages for that article.\n") # wikpd = wikipedia.page(str(wiki_var[0])) wiki_fact = wikipedia.summary(wiki_var, sentences=4) my_bot.reply_to(message, "<b>{0}.</b>\n{1}".format(wikp, wiki_fact), parse_mode="HTML") except wikipedia.exceptions.PageError: print("Page error for Wikipedia article\n{0}".format( str(wikp))) my_bot.reply_to(message, "<b>{0}.</b>".format("You're even unluckier"), parse_mode="HTML") except Exception as ex: exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] action_log( "Unknown Exception in Wikipedia: {}: {}\nat {} line {}". format(exc_type, ex, fname, exc_tb.tb_lineno)) except wikipedia.exceptions.PageError: print("Page error for Wikipedia article\n{0}".format(str(wikp))) my_bot.reply_to( message, "<b>{0}.</b>".format("You're very unlucky bastard"), parse_mode="HTML") except Exception as ex: exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] action_log( "Unknown Exception in Wikipedia: {}: {}\nat {} line {}".format( exc_type, ex, fname, exc_tb.tb_lineno))
async def on_message(message): print(message.author.name + "<" + message.content) reg_res = re.compile(u"#wea (.+)").search(message.content) voice_client = message.guild.voice_client vcl = f'{message.author.nick} ({message.author.name}#{message.author.discriminator})' if message.author.bot: return if '。' in message.content: return if 'いってき' in message.content: await message.channel.send('いってら!今日もがんばれ👍') if '勉強' in message.content: await message.channel.send('勉強がんばれ👍') if 'おは' in message.content: await message.channel.send('おは!今日も一日がんばれ👍') if 'おやす' in message.content: await message.channel.send('おう!おやすみ!睡眠がんばれ👍') if 'こんにちは' in message.content or message.content == 'こんちゃ' or message.content == 'こんちゃす' or message.content == 'こんちゃすー' or message.content == 'Hi' or message.content == 'Hello': await message.channel.send('こんちゃ!頑張ってるかい?応援するぜ!がんばれ👍') if message.content == 'こんばんは' or message.content == 'こんばんはー': await message.channel.send('こんばんは!まだ今日は終わってないぞ!がんばれ👍') if 'ただいま' in message.content[0:4]: await message.channel.send('おかえりぃ!頑張れたかい?') if 'がんば' in message.content or '頑張' in message.content: await message.channel.send('おう!俺も応援するぜ!がんばれ👍!') if message.content == '@がんばれ君': await message.channel.send('ん?どした?') if message.content == 'がんばった' or message.content == 'がんばったよ': await message.channel.send('よく頑張った!今後もがんばれ👍') if 'ハハッ' in message.content or 'ハハッ' in message.content or 'ははっ' in message.content or 'はハッ' in message.content or 'はハっ' in message.content or 'ははッ' in message.content or 'ハはっ' in message.content or 'ハはッ' in message.content or 'ハハっ' in message.content: await message.channel.send('(ミッキーだよ)') if '#pls' in message.content: plus_list_str = message.content.split() plus_list_str.remove('#pls') plus_list = map(float, plus_list_str) plus = sum(plus_list) await message.channel.send(plus) if '#mns' in message.content: minus_list2_str = message.content.split() minus_list2_str.remove('#mns') minus11 = float(minus_list2_str[0]) minus1 = Decimal(minus11) minus_list2_str.remove(minus_list2_str[0]) minus_list2 = map(float, minus_list2_str) minus_list = [i * -1 for i in minus_list2] minus22 = sum(minus_list) minus2 = Decimal(minus22) minus = minus1 + minus2 await message.channel.send(minus) if '#tim' in message.content: time_list_str = message.content.split() time_list_str.remove('#tim') time_list = map(float, time_list_str) time = reduce(mul, time_list) await message.channel.send(time) if '#div' in message.content: divide_list_str = message.content.split() divide_list_str.remove('#div') divide11_str = divide_list_str[0] divide11 = float(divide11_str) divide1 = Decimal(divide11) divide_list = map(float, divide_list_str) divide22 = reduce(mul, divide_list) divide2 = Decimal(divide22) divide = (divide1 / divide2) * divide1 await message.channel.send(divide) if '#oio' in message.content: oio0, oio1_str, oio2_str = message.content.split() oio1 = float(oio1_str) oio2 = float(oio2_str) oio = oio1 % oio2 ii = oio1 // oio2 iioio = f'{ii}あまり{oio}' await message.channel.send(iioio) if '#sqr' in message.content: square0, square1_str, square2_str = message.content.split() square1 = float(square1_str) square2 = float(square2_str) square = square1**square2 await message.channel.send(square) if '#rot' in message.content: root0, root1_str = message.content.split() root1 = float(root1_str) root2 = math.sqrt(root1) root = f'√{root1}, {root2}' await message.channel.send(root) if '#now' in message.content: await message.channel.send(nowa) if '#help' in message.content: embed = discord.Embed(title="がんばれ君が助けに来た!") embed.add_field(name="``応答``", value="たまに言葉で反応するときがあるよ!(「。」を使えば黙らせられるよー)", inline=False) embed.add_field(name="``#pls x y``", value="足し算できるよ!3個以上の数値もできるよ!(この場合はx+yになるよー)", inline=False) embed.add_field(name="`#mns x y`", value="引き算できるよ!3個以上の数値もできるよ!(この場合はx-yになるよー)", inline=False) embed.add_field(name="`#tim x y`", value="掛け算できるよ!3個以上の数値もできるよ!(この場合はx×yになるよー)", inline=False) embed.add_field(name="`#div x y`", value="割り算できるよ!3個以上の数値もできるよ!(この場合はx÷yになるよー)", inline=False) embed.add_field(name="`#oio x y`", value="割り算あまりできるよ!", inline=False) embed.add_field(name="`#sqr x y`", value="累乗できるよ!(この場合はxのy乗になるよー)", inline=False) embed.add_field(name="`#rot x`", value="ルートの値求めてくれるよ!", inline=False) embed.add_field(name="`#llt x y z`", value="ルーレットできるよ!(この場合はx,y,z,のどれかが出るよ!", inline=False) embed.add_field(name="`#ebr`", value="鯖内のデータがわかるよ!", inline=False) embed.add_field(name="`#fjk`", value="くぁwせdrftgyふじこlp", inline=False) embed.add_field(name="`#wiki`", value="wikiで検索してくれるよ!", inline=False) embed.add_field(name="`#wach `", value="wikiでxの検索候補を10個表示してくれるよ!", inline=False) embed.add_field(name='`#ranks`', value='それぞれのみんはやのランクの人数を教えてくれるよ!', inline=False) embed.add_field(name='`#zikan`', value='タイマーを使えるよ!', inline=False) embed.add_field(name='`#wea`', value='天気予報が見れるよ!(都道府県でやってね!)', inline=False) embed.add_field(name='`#p`(音楽用)', value='音楽が流せるよ!', inline=False) embed.add_field(name='`#leave`(音楽用)', value='ボイチャにいるbotを切断できるよ!', inline=False) embed.add_field(name='`#stop`(音楽用)', value='流している音楽を止めれるよ!', inline=False) embed.add_field(name='`#embed`', value='埋め込みで送れるよ!', inline=False) await message.channel.send(embed=embed) if '#llt' in message.content: rlt_list = message.content.split() rlt_list.remove('#llt') rlt_result = random.choice(rlt_list) await message.channel.send(rlt_result) if '#ebr' in message.content: embed = discord.Embed(title='みんはや鯖データ') guild = message.guild ebr_all = guild.member_count ebr_user = sum(1 for member in guild.members if not member.bot) ebr_bot = sum(1 for member in guild.members if member.bot) embed.add_field(name='`メンバー数`', value=ebr_all) embed.add_field(name='`人数`', value=ebr_user) embed.add_field(name='`bot数`', value=ebr_bot) embed.add_field(name='テキストチャンネル数', value=len(message.guild.text_channels), inline=False) embed.add_field(name='ボイスチャンネル数', value=len(message.guild.voice_channels), inline=False) embed.add_field(name='カテゴリー数', value=len(message.guild.categories), inline=False) await message.channel.send(embed=embed) if '!d bump' in message.content: if message.content.startswith("!d bump"): if client.user != message.author: def checks(m): return m.channel == message.channel and m.author.id == 302050872383242240 bp = await client.wait_for('message', check=checks, timeout=15) msgid = bp.id embmsg = await message.channel.fetch_message(msgid) bumpdata = "EmbedProxy(url='https://disboard.org/images/bot-command-image-bump.png', proxy_url='https://images-ext-1.discordapp.net/external/tAuRcs-FCy2M8OaTS9Ims62J1vrFiviahjBDtpZrrBs/https/disboard.org/images/bot-command-image-bump.png', width=800, height=200)" getdata = embmsg.embeds[0].image if str(bumpdata) == str(getdata): await asyncio.sleep(7200) embed = discord.Embed(title="BUMPできるよ!", description="BUMPがんばれ👍!", color=0x24B8B8) await message.channel.send(embed=embed) print("send:bump!!!") if '#ranks' in message.content: embed = discord.Embed(title='**ランクごとの人数!**') guild = message.guild role_S2 = guild.get_role(774989846501654528) embed.add_field(name='`S2ランク`', value=len(role_S2.members), inline=False) role_S1 = guild.get_role(774987289045630997) embed.add_field(name='`S1ランク`', value=len(role_S1.members), inline=False) role_S = guild.get_role(774989364199424010) embed.add_field(name='`Sランク`', value=len(role_S.members), inline=False) role_Ap = guild.get_role(774988208895033425) embed.add_field(name='`A+ランク`', value=len(role_Ap.members), inline=False) role_A = guild.get_role(774987300420583475) embed.add_field(name='`Aランク`', value=len(role_A.members), inline=False) role_Am = guild.get_role(774988863378030603) embed.add_field(name='`A-ランク`', value=len(role_Am.members), inline=False) role_Bp = guild.get_role(774988447676235797) embed.add_field(name='`B+ランク`', value=len(role_Bp.members), inline=False) role_B = guild.get_role(774988378596835339) embed.add_field(name='`Bランク`', value=len(role_B.members), inline=False) role_Bm = guild.get_role(774988334509326337) embed.add_field(name='`B-ランク`', value=len(role_Bm.members), inline=False) role_Cp = guild.get_role(774988120100700211) embed.add_field(name='`C+ランク`', value=len(role_Cp.members), inline=False) role_C = guild.get_role(774988030590058526) embed.add_field(name='`Cランク`', value=len(role_C.members), inline=False) role_Cm = guild.get_role(774987915004477470) embed.add_field(name='`C-ランク`', value=len(role_Cm.members), inline=False) await message.channel.send(embed=embed) if message.content.startswith('#ebons'): guild = message.guild ebr_all = guild.member_count ebr_user = sum(1 for member in guild.members if not member.bot) ebr_bot = sum(1 for member in guild.members if member.bot) ebr_alls = f'メンバー数:{ebr_all}' ebr_users = f'人数:{ebr_user}' ebr_bots = f'bot数:{ebr_bot}' new_channel = await create_channel(message, channel_name=ebr_alls) new_channel = await create_channel(message, channel_name=ebr_users) new_channel = await create_channel(message, channel_name=ebr_bots) if '#fjk' in message.content: await message.channel.send('くぁwせdrftgyふじこlp') if client.user in message.mentions: await reply(message) if '#zikan' in message.content: await zikan(message) if '#wiki' in message.content: wiki1 = message.content[6:] wikipedia.set_lang('ja') try: page_title = wikipedia.page(wiki1) embed = discord.Embed(title=wiki1, url=f'https://ja.wikipedia.org/wiki/{wiki1}') page_summary = wikipedia.summary(wiki1) embed.add_field(name=page_title, value=page_summary, inline=False) await message.channel.send(embed=embed) except wikipedia.exceptions.DisambiguationError: page_search = wikipedia.search(wiki1, results=11) page_search_url = f'https://ja.wikipedia.org/wiki/{page_search}' embed = discord.Embed() for page in page_search: page_int = page_search.index(page) page_url = f'https://ja.wikipedia.org/wiki/{page}' embed.add_field(name=page, value=f'「{page}」で再検索', inline=False) await message.channel.send(embed=embed) except wikipedia.exceptions.PageError: await message.channel.send('ページが見つからん!') if nowtime == '15:00': await timer() if "#embed" in message.content: embl = message.content.split() embl.remove('#embed') embls = ['' if i == '改行' else i for i in embl] tit = '\n'.join(embls) embed = discord.Embed(title='**{}**'.format(tit)) await message.channel.send(embed=embed) if '#ngadd' in message.content: await NG(message) if '#nglist' in message.content: embed = discord.Embed(title='NGワード一覧', description='このリスト内のワードは言っちゃだめだよ!') with open('NG') as ng: ngl = ng.read() fs = ngl.splitlines() for f in fs: embed.add_field(name=f, value=f, inline=False) await message.channel.send(embed=embed) if '#wach' in message.content: re.sub('#wiki', '', message.content) wikipedia.set_lang('ja') page_ach = wikipedia.search(message.content, results=11) page_search_url = f'https://ja.wikipedia.org/wiki/{page_ach}' embed = discord.Embed() for pages in page_ach: pages_int = page_ach.index(pages) pages_url = f'https://ja.wikipedia.org/wiki/{pages}' embed.add_field(name=pages, value=f'「{pages}」で再検索', inline=False) await message.channel.send(embed=embed) if message.channel.name == '自己紹介': yorosiku = "<:yorosiku:884506700126752828>" ok = "<:OK:884506700126752828>" await message.add_reaction(yorosiku) await message.add_reaction(ok) if message.content == "#hb": embed = discord.Embed( title='Hit&Browの遊び方', description= '相手の思っている数字を推理して当てるゲームだよ!\n数字と場所があってたら「Hit」、\n数字があっていても場所が違っていたら「Brow」でカウントするよ!\n最終的に3Hitにすれば勝ちだよ!' ) embed.add_field(name='#hs', value='ゲームを始めるよ!', inline=False) embed.add_field(name='#hc', value='あってるか確認するよ!', inline=False) embed.add_field(name='#hd', value='どうしてもわからないときに使ってね!(答えが出るよ)', inline=False) await message.channel.send(embed=embed) if message.content == '#hs': if message.channel.id in rooms: await message.channel.send('使用中なう') return rooms[message.channel.id] = Room() await message.channel.send('スタート!') if (message.content[0:3] == "#hc") and message.channel.id in rooms: req = message.content[3:] req = req.replace(" ", "") if len(req) != 4: await message.channel.send('4桁の番号だよ!') return hit, brow = rooms[message.channel.id].step(req) rooms[message.channel.id].history.append({ 'request': req, 'hit': hit, 'brow': brow }) await message.channel.send('リクエスト:' + req + '\n結果:{}ヒット {}ブロー'.format(hit, brow)) if req == rooms[message.channel.id].ans: await message.channel.send('正解!') say = '今までの記録だよ!\n質問回数:{}回| 数字 | ヒット | ブロー |\n'.format( len(rooms[message.channel.id].history)) for i in rooms[message.channel.id].history: say = say + '| {} | {} | {} |\n'.format( i['request'], i['hit'], i['brow']) await message.channel.send(say) del rooms[message.chanenl.id] if message.content == '#hd' and message.channel.id in rooms: await message.channel.send('ゲーム終了!答え:' + rooms[message.channel.id].ans) del rooms[message.channel.id] if message.content == '#hy' and message.channel.id in rooms: say = '今までの記録だよ!\n質問回数:{}回| 数字 | ヒット | ブロー |\n'.format( len(rooms[message.channel.id].history)) for i in rooms[message.channel.id].history: say = say + '| {} | {} | {} |\n'.format( i['request'], i['hit'], i['brow']) await message.channel.send(say) elif message.content == '#leave': if message.guild.voice_client is None: await message.channel.send("おーっと、ボイスチャンネルにいないからできないようだ!") return queue_list = [] await message.guild.voice_client.disconnect() await message.channel.send("バイバイ!") elif message.content.startswith('#pl'): try: if message.author.voice is None: await message.channel.send('おーっと、ボイスチャンネルにいないからできないようだ!') if message.guild.voice_client is None: await message.author.voice.channel.connect() if message.guild.voice_client.is_playing(): url = message.content[4:] player = await YTDLSource.from_url(url, loop=client.loop) queue_list.append(player) melo = queue_list[0] await message.channel.send('おーっと、再生中のようだ!') for i in queue_list: embed = discord.Embed(title='キュー') embed.add_field(name=i, value=vcl, inline=False) await message.channel.send(embed=embed) while len(queue_list) != 0: while message.guild.voice_client.is_playing(): await asyncio.sleep(1) await message.channel.send('``{}`` を再生!'.format(melo)) await message.guild.voice_client.play(melo) queue_list.remove(melo) url = message.content[4:] player = await YTDLSource.from_url(url, loop=client.loop) await message.channel.send('``{}`` を再生!'.format(player.title)) message.guild.voice_client.play(player) if message.content == '#np': if not message.guild.voice_client.is_playing(): await message.channel.send("おーっと、再生してないからできないようだ!") return embed = discord.Embed(title=player.title, url=player) await message.channel.send(embed=embed) except youtube_dl.utils.DownloadError: await message.channel.send('NOT FOUND!') if message.content == '#loop' and message.guild.voice_client.is_playing(): await message.channel.send('るーぷ!') player = message.guild.voice_client.is_playing() while message.guild.voice_client.is_playing(): await asyncio.sleep(0.1) while not message.content == "#lost" and message.guild.voice_client.is_playing( ): while True: message.guild.voice_client.play(player) await message.channel.send('るーぷ終了!') elif message.content == "#stop": if message.guild.voice_client is None: await message.channel.send("おーっと、ボイスチャンネルにいないからできないようだ!") return if not message.guild.voice_client.is_playing(): await message.channel.send("おーっと、再生してないからできないようだ!") return message.guild.voice_client.stop() await message.channel.send("停止...") if '#wel' in message.content: s = f"https://weather.tsukumijima.net/primary_area.xml" await message.channel.send(s) if reg_res: if reg_res.group(1) in citycodes.keys(): citycode = citycodes[reg_res.group(1)] resp = urllib.request.urlopen( f"https://weather.tsukumijima.net/api/forecast/city/{citycode}" ).read() resp = json.loads(resp.decode("utf-8")) msg = "__【お天気情報:**" + resp["location"]["city"] + "**】__\n" for f in resp["forecasts"]: msg += f["dateLabel"] + ":**" + f[ "telop"] + "**" + " 最高気温" + ":**" + str( f["temperature"]["max"] ["celsius"]) + "℃**" + " 最低気温" + ":**" + str( f["temperature"]["min"]["celsius"]) + "℃**\n" msg += "```" + resp["description"]["bodyText"] + "```" await message.channel.send(msg) else: await message.channel.send("そこの天気はわかりません...")
import wikipedia as wiki import sys #input(search_term=sys.argv[1]) search_term = str(sys.argv[1]) log_file = str(sys.argv[2]) search = wiki.search(search_term, results=1, suggestion=True) search_page = wiki.page(search[0][0]) print(search_page.url) f = open(log_file, "a+") f.write(str(search_page.url) + "\n") f.close()
def get_wiki(query): title = wikipedia.search(query)[0] page = wikipedia.page(title) return page.content
import wikipedia wikipedia.set_lang("ru") while True: try: search = input("Что искать?\n") if search == 'стоп': break s = wikipedia.page(search) print(s.content) print("Ссылка: " + s.url) except: print("По вашему запросу ничего не найдено!") print() print("Допустимые варианты:") print("====================") valid_list = wikipedia.search(search) for item in valid_list: print(item) print("====================") print()