Search code examples
pythonpython-3.xpython-dateutil

TypeError: parse() missing 1 required positional argument: 'timestr'


I am trying to scrape a website, but I keep getting the error that is in the title of this post. I have not figured out a way to solve this issue, and would greatly appreciate any help. Here is my code:

import requests
import json
from dateutil.parser import parser

url = 'website url'
info = requests.get(url)
data = info.json()

for entry in data['properties']['periods']:
    t = entry['startTime']
    print(parser.parse(t))

The website that I am trying to scrape is a weather forecast API, in a JSON format. The 'properties', 'periods' and 'startTime' are categories in the JSON. The funny thing is that when I feed the parser the value that is stored in these categories, it works seamlessly, but not when the value is a variable. What am I doing wrong?

Example Data


Solution

  • parser is a class:

    class parser(object):
       def __init__(self, info=None):
           self.info = info or parserinfo()
    
       def parse(self, timestr, default=None,
                 ignoretz=False, tzinfos=None, **kwargs):
           [...]
    

    When you do something like parser.parse(t) you are passing t as the self argument and the required positional argument timestr doesn't get a value. You need to call this method on an instance:

    parser().parse(t)
    

    And since you're using it in a loop, it would be better to create it once before the loop:

    date_parser = parser()
    for entry in data['properties']['periods']:
        t = entry['startTime']
        print(date_parser.parse(t))