pygooglenews

repository·master·Indexed 23 days ago

https://github.com/kotartemiy/pygooglenews

A Python wrapper for the Google News RSS feed that allows developers to fetch top stories, topic-based news, and geolocation-specific news. It includes a search method supporting advanced Google Search operators, time-based filtering, and date ranges. The library supports the use of custom HTTP/HTTPS proxies or ScrapingBee API keys to avoid IP locking in production environments.

Tokens
2.8K
Snippets
12
Records
19
Agent score
30%

What's inside pygooglenews

  1. Use Google Search special query terms in search()

    master

    You can use several Google Search operators within the query parameter of search() to refine results:

    • Boolean OR: boeing OR airbus
    • Exclude term: Use - before a word (e.g., apple -fruit)
    • Include term: Use + before a word (e.g., +apple)
    • Phrase Search: Use double quotes (e.g., "Tokyo Olympics")
    • allintext:: Requires all words to be in the document body.
    • intitle:: Restricts results to documents containing a word in the title.
    • allintitle:: Restricts results to documents containing all query words in the title.
    • inurl:: Restricts results to documents containing a word in the URL.
    • allinurl:: Restricts results to documents containing all query words in the URL.

    Note: Operators like as_*, allinlinks:, and related: are reported as not working with Google News.

    from pygooglenews import GoogleNews
    gn = GoogleNews()
    s = gn.search('boeing OR airbus')
  2. Understand the output data format

    master

    All primary retrieval functions (top_news, topic_headlines, geo_headlines, and search) return a dictionary containing two main sub-objects:

    1. feed: A FeedParserDict containing metadata about the feed. You can check feed['title'] to see the name of the feed.
    2. entries: A list of parsed articles. Each entry in this list also includes a sub_articles key, which contains similar articles found in the description (this is typically non-empty for top_news() and topic_headlines() results).
  3. Use ScrapingBee or Proxies for Production

    master

    To avoid IP locking by Google when running in production, you can pass credentials to the library's methods to use a proxy service.

    1. ScrapingBee: Pass your API key using the scraping_bee parameter in any function call.
    2. Custom Proxies: Pass a Python dictionary of proxies using the proxies parameter in any function call.
  4. Search for news using gn.search()

    master

    The search() method allows for extensive query support, including exact matches, title/URL matches, and date filtering. It handles URL-escaping of user input automatically.

    Supported parameters:

    • when: Filter by time period (e.g., '6m' for the past 6 months).
    • from_: Search from a specific date.
    • to_: Search up to a specific date.

    Note: The search function supports boolean-style operators (e.g., using - to exclude terms).

    # search for the best matching articles that mention MSFT and 
    # do not mention AAPL (over the past 6 month
    search = gn.search('MSFT -APPL', when = '6m')
  5. Perform advanced queries with gn.search()

    master

    The GoogleNews.search() method supports advanced Google News search operators. You can use standard operators like - for exclusion, intitle: to restrict searches to titles, and OR for logical disjunction. Additionally, you can use the when parameter to filter results by time (e.g., '1h' for the past hour).

    from pygooglenews import GoogleNews
    
    gn = GoogleNews()
    
    # Search for articles that mention 'boeing' and do not mention 'airbus'
    s = gn.search('boeing -airbus')
    
    # Search for articles that mention 'boeing' in title
    s = gn.search('intitle:boeing')
    
    # Search for articles that mention 'boeing' in title and published over the past hour
    s = gn.search('intitle:boeing', when = '1h')
    
    # Search for articles that mention 'boeing' or 'airbus' within the last hour
    s = gn.search('boeing OR airbus', when = '1h')
  6. Initialize the GoogleNews class

    master

    To use pygooglenews, you must first instantiate the GoogleNews class. It requires two parameters: lang (language) and country. Note that only combinations supported by Google News will work. You can verify supported combinations on the official Google News page under the 'Language & region' section.

    Example: For Ukraine, you might use lang='uk' and country='UA' or lang='ru' and country='UA'.

    from pygooglenews import GoogleNews
    # default GoogleNews instance
    gn = GoogleNews(lang = 'en', country = 'US')
  7. Get Top Stories, Topic Headlines, and Geolocation News

    master

    The GoogleNews instance provides methods to fetch specific types of news feeds:

    • top_news(): Fetches the current top stories.
    • topic_headlines(topic): Fetches news related to a specific topic (e.g., 'business').
    • geo_headlines(location): Fetches news specific to a geolocation (e.g., 'San Fran').
    from pygooglenews import GoogleNews
    
    gn = GoogleNews()
    
    # Top Stories
    top = gn.top_news()
    
    # Stories by Topic
    business = gn.topic_headlines('business')
    
    # Geolocation Specific Stories
    headquaters = gn.geo_headlines('San Fran')
  8. Get stories by topic

    master

    The topic_headlines() method retrieves news for specific predefined topics.

    Accepted topics include:

    • WORLD
    • NATION
    • BUSINESS
    • TECHNOLOGY
    • ENTERTAINMENT
    • SCIENCE
    • SPORTS
    • HEALTH

    You can also use custom topic IDs. To find a topic ID, navigate to the Google News topic URL (e.g., https://news.google.com/topics/[TOPIC_ID]?hl=en-US...) and copy the string between topics/ and the ? character. Note that topic IDs are unique to each language/country combination.

    The returned object contains a feed (FeedParserDict) and an entries list of parsed articles.

    # Using a predefined topic
    business = gn.topic_headlines('BUSINESS', proxies=None, scraping_bee = None)
    
    # Using a custom topic ID
    from pygooglenews import GoogleNews
    gn = GoogleNews()
    covid = gn.topic_headlines('CAAqIggKIhxDQkFTRHdvSkwyMHZNREZqY0hsNUVnSmxiaWdBUAE')