py_alpaca_api.trading.news

Attributes

logger

yfinance_logger

START_DATE

END_DATE

Classes

Requests

News

Module Contents

class py_alpaca_api.trading.news.Requests[source]
request(method: str, url: str, headers: dict[str, str] | None = None, params: dict[str, str | bool | float | int] | None = None, json: dict[str, Any] | None = None, raw_response: bool = False)[source]

Execute HTTP request with retry logic.

Parameters:
  • method – A string representing the HTTP method to be used in the request.

  • url – A string representing the URL to send the request to.

  • headers – An optional dictionary containing the headers for the request.

  • params – An optional dictionary containing the query parameters for the request.

  • json – An optional dictionary containing the JSON payload for the request.

  • raw_response – If True, return the raw response object without status checks. Defaults to False.

Returns:

The response object returned by the server.

Raises:

APIRequestError – If the response status code is not one of the acceptable statuses (200, 204, 207) and raw_response is False.

py_alpaca_api.trading.news.logger[source]
py_alpaca_api.trading.news.yfinance_logger[source]
py_alpaca_api.trading.news.START_DATE[source]
py_alpaca_api.trading.news.END_DATE[source]
class py_alpaca_api.trading.news.News(headers: dict[str, str])[source]
static strip_html(content: str)[source]

Removes HTML tags and returns the stripped content.

Parameters:

content (str) – The HTML content to be stripped.

Returns:

The stripped content without HTML tags.

Return type:

str

static scrape_article(url: str) str | None[source]

Scrapes the article text from the given URL.

Parameters:

url (str) – The URL of the article.

Returns:

The text content of the article, or None if the article body is not found.

Return type:

str | None

static truncate(text: str, length: int) str[source]

Truncates a given text to a specified length.

Parameters:
  • text (str) – The text to be truncated.

  • length (int) – The maximum length of the truncated text.

Returns:

The truncated text.

Return type:

str

get_news(symbol: str, limit: int = 6) list[dict[str, str]][source]

Retrieves news articles related to a given symbol from Benzinga and Yahoo Finance.

Note: Yahoo Finance has implemented anti-scraping measures that prevent fetching full article content. Yahoo news will include title, URL, publish date, and summary/description when available, but not full article text.

Parameters:
  • symbol (str) – The symbol for which to retrieve news articles.

  • limit (int, optional) – The maximum number of news articles to retrieve. Defaults to 6.

Returns:

A list of news articles, sorted by publish date in descending order.

Return type:

list