Code
from gnews import GNews
import newspaper
import base64
import requests
import pandas as pd
Testing the Google News API for extracting news articles
Imports
from gnews import GNews
import newspaper
import base64
import requests
import pandas as pd
Initialise the API with German Settings and 7 days period
= GNews(
gnews ='de', # language of the news articles
language='DE', # country of the news articles
country='7d' # time period of the news articles (last 7 days)
period )
Run test with keyword Ukraine EU
= gnews.get_news(
news 'Ukraine EU' # search query
)
Display Output using pandas (only 10 of 33 articles)
= pd.DataFrame(news)
df_news # Expand the publisher object to 3 columns
= df_news['publisher'].apply(pd.Series)
df_pub 'pub_url', 'pub_title']] = df_pub
df_news[[=['publisher'], inplace=True)
df_news.drop(columns10) df_news.head(
title | description | published date | url | pub_url | pub_title | |
---|---|---|---|---|---|---|
0 | Brüssel - EU-Staaten geben grünes Licht für ne... | Brüssel - EU-Staaten geben grünes Licht für ne... | Tue, 06 Aug 2024 14:28:01 GMT | https://news.google.com/rss/articles/CBMirgFBV... | https://www.deutschlandfunk.de | Deutschlandfunk |
1 | Ukrainekrieg: Tausende Menschen fliehen aus ru... | Ukrainekrieg: Tausende Menschen fliehen aus ru... | Mon, 05 Aug 2024 09:42:20 GMT | https://news.google.com/rss/articles/CBMicEFVX... | https://www.zeit.de | zeit.de |
2 | Ukraine-Liveticker: Medienberichte: Youtube in... | Ukraine-Liveticker: Medienberichte: Youtube in... | Thu, 01 Aug 2024 18:59:17 GMT | https://news.google.com/rss/articles/CBMixAFBV... | https://www.faz.net | FAZ - Frankfurter Allgemeine Zeitung |
3 | Getreideexport aus der Ukraine läuft auf Hocht... | Getreideexport aus der Ukraine läuft auf Hocht... | Sat, 03 Aug 2024 03:15:22 GMT | https://news.google.com/rss/articles/CBMingFBV... | https://www.agrarheute.com | agrarheute.com |
4 | Putins Krieg in der Ukraine: Das sind die Entw... | Putins Krieg in der Ukraine: Das sind die Entw... | Tue, 06 Aug 2024 15:42:00 GMT | https://news.google.com/rss/articles/CBMivAFBV... | https://www.suedkurier.de | SÜDKURIER Online |
5 | Ukraine: Hoffnung auf weitere EU-Agrarhilfen -... | Ukraine: Hoffnung auf weitere EU-Agrarhilfen ... | Wed, 07 Aug 2024 12:33:00 GMT | https://news.google.com/rss/articles/CBMinwFBV... | https://www.agrarzeitung.de | agrarzeitung online |
6 | Mützenich erwartet Debatte um Ukraine-Territor... | Mützenich erwartet Debatte um Ukraine-Territor... | Thu, 01 Aug 2024 06:06:00 GMT | https://news.google.com/rss/articles/CBMiowFBV... | https://www.politico.eu | POLITICO Europe |
7 | Energiekrise droht EU-Land – Ukraine nach Sank... | Energiekrise droht EU-Land – Ukraine nach Sank... | Sun, 04 Aug 2024 03:02:00 GMT | https://news.google.com/rss/articles/CBMitgFBV... | https://www.fr.de | fr.de |
8 | Ukraine - top agrar online | Ukraine top agrar online | Thu, 01 Aug 2024 07:00:00 GMT | https://news.google.com/rss/articles/CBMiY0FVX... | https://www.topagrar.com | top agrar online |
9 | Selenski will Gebiet nur mit Zustimmung des Vo... | Selenski will Gebiet nur mit Zustimmung des Vo... | Fri, 02 Aug 2024 06:28:00 GMT | https://news.google.com/rss/articles/CBMizAFBV... | https://www.handelsblatt.com | Handelsblatt |
Looks promising, but the URL is encoded. We need to decode it to get the full text of the article
decode the URL using this comment on GitHub
import base64
import requests
def fetch_decoded_batch_execute(id):
= (
s '[[["Fbv4je","[\\"garturlreq\\",[[\\"en-US\\",\\"US\\",[\\"FINANCE_TOP_INDICES\\",\\"WEB_TEST_1_0_0\\"],'
'null,null,1,1,\\"US:en\\",null,180,null,null,null,null,null,0,null,null,[1608992183,723341000]],'
'\\"en-US\\",\\"US\\",1,[2,3,4,8],1,0,\\"655000234\\",0,0,null,0],\\"' +
id +
'\\"]",null,"generic"]]]'
)
= {
headers "Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
"Referer": "https://news.google.com/"
}
= requests.post(
response "https://news.google.com/_/DotsSplashUi/data/batchexecute?rpcids=Fbv4je",
=headers,
headers={"f.req": s}
data
)
if response.status_code != 200:
raise Exception("Failed to fetch data from Google.")
= response.text
text = '[\\"garturlres\\",\\"'
header = '\\",'
footer if header not in text:
raise Exception(f"Header not found in response: {text}")
= text.split(header, 1)[1]
start if footer not in start:
raise Exception("Footer not found in response.")
= start.split(footer, 1)[0]
url return url
def decode_google_news_url(source_url):
= requests.utils.urlparse(source_url)
url = url.path.split("/")
path if url.hostname == "news.google.com" and len(path) > 1 and path[-2] == "articles":
= path[-1]
base64_str = base64.urlsafe_b64decode(base64_str + '==')
decoded_bytes = decoded_bytes.decode('latin1')
decoded_str
= b'\x08\x13\x22'.decode('latin1')
prefix if decoded_str.startswith(prefix):
= decoded_str[len(prefix):]
decoded_str
= b'\xd2\x01\x00'.decode('latin1')
suffix if decoded_str.endswith(suffix):
= decoded_str[:-len(suffix)]
decoded_str
= bytearray(decoded_str, 'latin1')
bytes_array = bytes_array[0]
length if length >= 0x80:
= decoded_str[2:length+1]
decoded_str else:
= decoded_str[1:length+1]
decoded_str
if decoded_str.startswith("AU_yqL"):
return fetch_decoded_batch_execute(base64_str)
return decoded_str
else:
return source_url
Now Extract the full text of the article
= decode_google_news_url(news[3]['url'])
url = newspaper.article(url) art
Take a look at the original article here to compare it with the extracted info
print(f"Titel: {art.title}")
print(f"Autor: {art.authors}")
print(f"Datum: {art.publish_date}")
Titel: Getreideexport aus der Ukraine läuft auf Hochtouren - auch in die EU
Autor: ['Norbert Lehmann']
Datum: 2024-08-03 05:15:22+02:00
The Full Text of teh Article (only first 400 characters)
print(art.text[:400]+"..")
In der Ukraine wird gerade die dritte Getreideernte seit Beginn des russischen Überfalls eingebracht. Zu Beginn des neuen Wirtschaftsjahres 2024/25 fließen die Ausfuhren an Weizen, Gerste und Mais deutlich schneller ab als vor einem Jahr. Das liegt auch daran, dass der Frachtverkehr über das Schwarze Meer weniger stark gestört ist als noch 2023.
Nach Angaben der nationalen Zollbehörde exportierte..
Recognizes all Parameters of the given article (author, title, date) and extracts the full text. The API is ready for further use, e.g doing a sentiment analysis on the text.