def get_admin_data(user_headline, user_img, user_keywords): admin_data = {'link': None, 'headline': None, 'content': None, 'image': None} google_news = GoogleNews(lang='en') google_news.search(user_headline) links = google_news.get__links() print('No. of links found: ', len(links)) if len(links) == 0: google_news = GoogleNews(lang='en') google_news.search(' '.join(user_keywords)) links2 = google_news.get__links() if len(links2) == 0: return admin_data else: links = links2 if len(links) == 1: link_used = links[0] else: link_used = links[1] admin_data['link'] = link_used # print(link_used) article = Article(link_used) article.download() article.parse() article.nlp() admin_data['headline'] = article.title admin_data['content'] = article.summary if article.top_image is not None: admin_data['image'] = article.top_image print('admin link: ', admin_data['link']) print('admin headline: ', admin_data['headline']) return admin_data
def get_admin_data(user_headline, user_img): admin_data = { 'link': None, 'headline': None, 'content': None, 'image': None } google_news = GoogleNews(lang='en') google_news.search(user_headline) links = google_news.get__links() print('No. of links found: ', len(links)) if len(links) == 0: return admin_data elif len(links) == 1: link_used = links[0] else: link_used = links[1] admin_data['link'] = link_used print(link_used) article = Article(link_used) article.download() article.parse() article.nlp() admin_data['headline'] = article.title admin_data['content'] = article.summary if article.top_image is None: admin_data['image'] = user_img else: admin_data['image'] = article.top_image return admin_data
def search(): global state, config if config is None: raise Exception('Call initiateConfig first') if state is None: state = {} state['url'] = {} googlenews = GoogleNews() googlenews = GoogleNews('en', 'd') for city in config['cities']: googlenews.search('covid in ' + city) state['url'][city] = [] for i in range(config['pagesPerCity']): googlenews.getpage(i) state['url'][city].extend(googlenews.get__links())
def run(start_date, end_date, keyword, file, mail, importance): #find relevant news articles within given timeframe googlenews = GoogleNews() googlenews = GoogleNews(lang='en') googlenews = GoogleNews(start=start_date, end=end_date) googlenews.search(keyword) res = googlenews.result() googlenews.result() headlines = googlenews.gettext() links = googlenews.get__links( ) #note that documentation has this as googlenews.getlinks() so it might change #get page url results = articleReader(links, headlines, keyword) run.df = pd.DataFrame(results) if run.df.shape[0] > importance: run.df = run.df.iloc[0:importance] return run.df
from GoogleNews import GoogleNews from datetime import date, datetime googlenews = GoogleNews() today = date.today() date = today.strftime("%d/%m/%Y") googlenews = GoogleNews(lang='en') googlenews = GoogleNews(period='d') googlenews.search(' ') news = googlenews.gettext() links = googlenews.get__links() #googlenews.getpage(1) #result = googlenews.result() for n in range(len(news)): print(news[n]) #print(links[n]) ''' for n in range(len(result)): print(n) for index in result[n]: print(index, '\n', result[n][index]) '''
print('============\nGOOGLE NEWS\n============') main_topic = input('Choose a topic: ') while run: try: gnews.search(main_topic) for n, result in enumerate(gnews.result()): print(n, result['title']) ## choose which article to pick article = input('\nChoose an article by its main index or choose [all] to visualize all links: ') if article == 'all': [print(f'{n}: {new}') for n, new in enumerate(gnews.gettext())] print('--') [print(f'{n}: {link}') for n, link in enumerate(gnews.get__links())] else: article = int(article) print(f'Article - Title: {gnews.gettext()[article]}') list_artcl = gnews.gettext() print(f'Article - Link: {gnews.get__links()[article]}') list_links = gnews.get__links() print('==========================================================================\n') go_further = input('DO you want to read other articles? (y/n) ').lower() if go_further == 'y' or go_further == 'yes': gnews.clear() ## before going in loop, needs to clears the article list else: run = False ## break the loop except: print('Error :S')
def getLinks(query, num_links=5): googlenews = GoogleNews(lang="en") googlenews.search(query) return googlenews.get__links()[:num_links]