python - 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?
解决方案
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))
推荐阅读
- sql - 如何对一列进行排名并在第二列中获得事件的运行总数?
- android - 签署 Android 白标应用程序
- angular - Angular CDK拖放添加新元素不移动
- c# - DataTable.Columns.Remove() 的时间/空间复杂度是多少?
- python - No Module Found python (在IDE之外运行)
- javascript - 禁用按钮,直到所有表单字段都填写在 Formik
- haskell - 我的最长公共子序列 Haskell 函数有什么问题?
- oauth-2.0 - Google OAuth2 Java 代码每次都询问权限
- html - 如何测试视口元标记的效果?
- ruby-on-rails - Ruby on rails 无法在 Windows 10 上运行