Provide an API to search for articles on Google News and returns a usable JSON response.
Project description
GNews
A Happy and lightweight Python Package that Provide an API to search for articles on Google News and returns a usable JSON response!
Explore the docs »
View Demo
·
Report Bug
·
Request Feature
Table of Contents
About GNews
🚩 GNews is A Happy and lightweight Python Package that searches Google News RSS Feed and returns a usable JSON
response
🚩 As well as you can fetch full article (No need to write scrappers for articles fetching anymore)
Google News cover across 141+ countries with 41+ languages. On the bottom left side of the Google News page you
may find a Language & region
section where you can find all of the supported combinations.
Demo
Getting Started
This is an example of how you may give instructions on setting up your project locally. To get a local copy up and running follow these simple example steps.
Installation
pip install gnews
Usage
Get news by keyword
GNews.get_news(keyword)
Get top news
GNews.get_top_news()
Get news by major topic
GNews.get_news_by_topic(topic)
- Available topics: WORLD, NATION, BUSINESS, TECHNOLOGY, ENTERTAINMENT, SPORTS, SCIENCE, HEALTH.
Get news by geo location
GNews.get_news_by_location(location)
- location can be name of city/state/country
Results specification
- It's possible to set country, language, period and size during initialization
google_news = GNews(language='en', country='US', period='7d', max_results=10)
- Or change it to an existing object
google_news.period = '7d' # News from last 7 days
google_news.results = 10 # number of responses across a keyword
google_news.country = 'United States' # News from a specific country
google_news.language = 'english' # News in a specific language
The format of the timeframe is a string comprised of a number, followed by a letter representing the time operator. For example 1y would signify 1 year. Full list of operators below:
- h = hours (eg: 12h)
- d = days (eg: 7d)
- m = months (eg: 6m)
- y = years (eg: 1y)
Supported Countries
print(google_news.countries)
{'Australia': 'AU', 'Botswana': 'BW', 'Canada ': 'CA', 'Ethiopia': 'ET', 'Ghana': 'GH', 'India ': 'IN',
'Indonesia': 'ID', 'Ireland': 'IE', 'Israel ': 'IL', 'Kenya': 'KE', 'Latvia': 'LV', 'Malaysia': 'MY', 'Namibia': 'NA',
'New Zealand': 'NZ', 'Nigeria': 'NG', 'Pakistan': 'PK', 'Philippines': 'PH', 'Singapore': 'SG', 'South Africa': 'ZA',
'Tanzania': 'TZ', 'Uganda': 'UG', 'United Kingdom': 'GB', 'United States': 'US', 'Zimbabwe': 'ZW',
'Czech Republic': 'CZ', 'Germany': 'DE', 'Austria': 'AT', 'Switzerland': 'CH', 'Argentina': 'AR', 'Chile': 'CL',
'Colombia': 'CO', 'Cuba': 'CU', 'Mexico': 'MX', 'Peru': 'PE', 'Venezuela': 'VE', 'Belgium ': 'BE', 'France': 'FR',
'Morocco': 'MA', 'Senegal': 'SN', 'Italy': 'IT', 'Lithuania': 'LT', 'Hungary': 'HU', 'Netherlands': 'NL',
'Norway': 'NO', 'Poland': 'PL', 'Brazil': 'BR', 'Portugal': 'PT', 'Romania': 'RO', 'Slovakia': 'SK', 'Slovenia': 'SI',
'Sweden': 'SE', 'Vietnam': 'VN', 'Turkey': 'TR', 'Greece': 'GR', 'Bulgaria': 'BG', 'Russia': 'RU', 'Ukraine ': 'UA',
'Serbia': 'RS', 'United Arab Emirates': 'AE', 'Saudi Arabia': 'SA', 'Lebanon': 'LB', 'Egypt': 'EG',
'Bangladesh': 'BD', 'Thailand': 'TH', 'China': 'CN', 'Taiwan': 'TW', 'Hong Kong': 'HK', 'Japan': 'JP',
'Republic of Korea': 'KR'}
Supported Languages
print(google_news.languages)
{'english': 'en', 'indonesian': 'id', 'czech': 'cs', 'german': 'de', 'spanish': 'es-419', 'french': 'fr',
'italian': 'it', 'latvian': 'lv', 'lithuanian': 'lt', 'hungarian': 'hu', 'dutch': 'nl', 'norwegian': 'no',
'polish': 'pl', 'portuguese brasil': 'pt-419', 'portuguese portugal': 'pt-150', 'romanian': 'ro', 'slovak': 'sk',
'slovenian': 'sl', 'swedish': 'sv', 'vietnamese': 'vi', 'turkish': 'tr', 'greek': 'el', 'bulgarian': 'bg',
'russian': 'ru', 'serbian': 'sr', 'ukrainian': 'uk', 'hebrew': 'he', 'arabic': 'ar', 'marathi': 'mr', 'hindi': 'hi',
'bengali': 'bn', 'tamil': 'ta', 'telugu': 'te', 'malyalam': 'ml', 'thai': 'th', 'chinese simplified': 'zh-Hans',
'chinese traditional': 'zh-Hant', 'japanese': 'ja', 'korean': 'ko'}
Article Properties
- Get news returns the list with following keys:
title
,published_date
,description
,url
,publisher
.
Properties | Description | Example |
---|---|---|
title | Title of the article | IMF Staff and Pakistan Reach Staff-Level Agreement on the Pending Reviews Under the Extended Fund Facility |
url | Google news link to article | Article Link |
published date | Published date | Wed, 07 Jun 2017 07:01:30 GMT |
description | Short description of article | IMF Staff and Pakistan Reach Staff-Level Agreement on the Pending Reviews Under the Extended Fund Facility ... |
publisher | Publisher of article | The Guardian |
Example usage
from gnews import GNews
google_news = GNews()
pakistan_news = google_news.get_news('Pakistan')
print(pakistan_news[0])
[{
'publisher': 'Aljazeera.com',
'description': 'Pakistan accuses India of stoking conflict in Indian Ocean '
'Aljazeera.com',
'published date': 'Tue, 16 Feb 2021 11:50:43 GMT',
'title': 'Pakistan accuses India of stoking conflict in Indian Ocean - '
'Aljazeera.com',
'url': 'https://www.aljazeera.com/news/2021/2/16/pakistan-accuses-india-of-nuclearizing-indian-ocean'
},
...]
Getting full article
- To read a full article you can either:
- Navigate to the url directly in your browser, or
- Use
newspaper3k
library to scrape the article
- The article url, needed for both methods, is accessed as
article['url']
.
Using newspaper3k
- Install the library -
pip3 install newspaper3k
. - Use
get_full_article
method fromGNews
, that creates annewspaper.article.Article
object from the url.
from gnews import GNews
google_news = GNews()
json_resp = google_news.get_news('Pakistan')
article = google_news.get_full_article(json_resp[0]['url']) # newspaper3k instance, you can access newspaper3k all attributes in article
This new object contains title
, text
(full article) or images
attributes.
Examples:
article.title
IMF Staff and Pakistan Reach Staff-Level Agreement on the Pending Reviews Under the Extended Fund Facility'
article.text
End-of-Mission press releases include statements of IMF staff teams that convey preliminary findings after a mission. The views expressed are those of the IMF staff and do not necessarily represent the views of the IMF’s Executive Board.\n\nIMF staff and the Pakistani authorities have reached an agreement on a package of measures to complete second to fifth reviews of the authorities’ reform program supported by the IMF Extended Fund Facility (EFF) ..... (full article)
article.images
{'https://www.imf.org/~/media/Images/IMF/Live-Page/imf-live-rgb-h.ashx?la=en', 'https://www.imf.org/-/media/Images/IMF/Data/imf-logo-eng-sep2019-update.ashx', 'https://www.imf.org/-/media/Images/IMF/Data/imf-seal-shadow-sep2019-update.ashx', 'https://www.imf.org/-/media/Images/IMF/Social/TW-Thumb/twitter-seal.ashx', 'https://www.imf.org/assets/imf/images/footer/IMF_seal.png'}
article.authors
[]
Read full documentation for newspaper3k
newspaper3k
Todo
- Save to MongoDB
- Save to SQLite
- Save to JSON
- Save to .CSV file
- More than 100 articles
Roadmap
See the open issues for a list of proposed features (and known issues).
Contributing
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
License
Distributed under the MIT License. See LICENSE
for more information.
Contact
Muhammad Abdullah - @ranahaani - ranahaani@gmail.com
Project Link: https://github.com/ranahaani/GNews
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file gnews-0.2.2.tar.gz
.
File metadata
- Download URL: gnews-0.2.2.tar.gz
- Upload date:
- Size: 14.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ced061852d23b28489997bac09fa4106d2e346e26a001b668844e8c9f9e060ed |
|
MD5 | 13d7928fd6d7b183b7eedeb6a12572fa |
|
BLAKE2b-256 | 05ce14013c2299b549a791633223732b1888a13e9071e35f51edbb59a0428aa1 |
Provenance
File details
Details for the file gnews-0.2.2-py3-none-any.whl
.
File metadata
- Download URL: gnews-0.2.2-py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c152da51d3da1dfb991d9d05eb40f0072855ab38fb456df443785b68858c6a5d |
|
MD5 | 2faf5d41baaa4c3dc7f47a8f90c6d3ba |
|
BLAKE2b-256 | 8a0b7d7bbf2bc39d17fc88066cc9b484a6c90560acc6e7feb48613d75836a2a6 |