'Python Polygon Stocks API

I am learning to grab some news with dates filtering, say before 2022-03-01, from Polygon Stock API with below code:

from polygon import RESTClient
client = RESTClient(my_API_key)
response = client.reference_ticker_news_v2( ticker='TSLA', published_utc.lte='2022-03-01')

But below error is found:

  File "<ipython-input-57-bdc0fdfab609>", line 1
    response = client.reference_ticker_news_v2( ticker='TSLA', published_utc.lte='2022-03-01')
                                                              ^
SyntaxError: keyword can't be an expression

https://polygon.io/docs/stocks/get_v2_reference_news

I followed the parameters in above link but seems there is error with published_utc.lte (or other date range filtering parameters). The above code works when I replace the parameter with exact date parameter published_utc = '2022-03-01'. Can anyone help with this? Many thanks.



Solution 1:[1]

As Bialomazur pointed out, published_utc.lte is not a valid keyword argument name.

From the Python API docs:

Every function call under our RESTClient has the query_params kwargs. These kwargs are passed along and mapped 1:1 as query parameters to the underling HTTP call.

So use a dictionary to pass those as keyword args.

query_params = {'ticker': 'TSLA',
                'published_utc.lte': '2022-03-01'}
response = client.reference_ticker_news_v2(**query_params)

Solution 2:[2]

I don't entirely understand where the problem is here. The second line of code is syntactically wrong as the error message shows.

published_utc.lte is not a valid keyword argument name. Keyword arguments must be valid identifiers. You should read up in the Python docs on this basic part of the syntactical rules of Python.

https://docs.python.org/3/reference/lexical_analysis.html

https://realpython.com/python-keywords/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Bialomazur