pythongoogle-trends

pytrends.request.TrendReq._get_data() got multiple values help wanted (Python)


I'm working on a script that looks at Google Trends and have hit a bit of a problem in regards to a variable called "params":

This is the code I have:

param = {
'hl': 'en-US',
}

response = requests.get(**INSERT URL HERE**, params=param)

class TrendReq(UTrendReq):
    def _get_data(self, url, method=GET_METHOD, trim_chars=0, **kwargs):
        return super()._get_data(url, method=GET_METHOD, trim_chars=trim_chars, params=param, **kwargs)

pt = TrendReq()

I'm getting the response:

TypeError: pytrends.request.TrendReq._get_data() got multiple values for keyword argument 'params'

I'm not 100% sure I'm using this whole bit of script correctly, but if there are any thoughts around the multiple values, I would appreciate any ideas. I've been attempting to follow the instructions on this page.


Solution

  • From the error description:

    "TypeError: got multiple values for argument" occurs when we overwrite the value of a positional argument with a keyword argument in a function call.

    Which means you have inadvertently created a positional argument called params in the _get_data() method, which itself gets called multiple times in the pytrends package from function calls that set params already:

    ...thus resulting in the conflict of two different params set.

    Solution:

    If all you need to set is accept-language header and the corresponding geo= URL parameter then simply define it when you instance the TrendReq parser:

    pytrends = TrendReq(hl='en-US', ...)
    

    Or don't set it at all, since hl='en-US' is set by default.