简体   繁体   English

TypeError:object()不带参数-Python 3.6.3

[英]TypeError: object() takes no parameters - Python 3.6.3

When I run $ python -m weatherterm -a USNY1192:1:US -p WeatherComParser -td to output the parsed data in terminal, I get following error: 当我运行$ python -m weatherterm -a USNY1192:1:US -p WeatherComParser -td在终端中输出解析后的数据时,出现以下错误:

Traceback (most recent call last):
  File "C:\Python36\lib\runpy.py", line 193, in _run_module_as_main "__main__", mod_spec)
  File "C:\Python36\lib\runpy.py", line 85, in _run_code
      exec(code, run_globals)
  File "C:\Users\Reza\weather-app\weatherterm\weatherterm\__main__.py", line 81, in <module>
      parser = cls()
  File "C:\Users\Reza\weather-app\weatherterm\weatherterm\parsers\weather_com_parser.py", line 25, in __init__
      self._request = Request(self._base_url)
TypeError: object() takes no parameters

Appreciate if you could advise what went wrong here: 如果您可以在这里提出问题,请感激:

from weatherterm.core import ForecastType
import re
from weatherterm.core import Forecast
from weatherterm.core import Request
from weatherterm.core import Unit
from weatherterm.core import UnitConverter

from bs4 import BeautifulSoup


class WeatherComParser:

    def __init__(self):
        self._forecast = {
            ForecastType.TODAY: self._today_forecast,
            ForecastType.FIVEDAYS: self._five_and_ten_days_forecast,
            ForecastType.TENDAYS: self._five_and_ten_days_forecast,
            ForecastType.WEEKEND: self._weekend_forecast,
        }

        self._base_url = 'http://weather.com/weather/{forecast}/l/{area}'
        self._request = Request(self._base_url)

        self._temp_regex = re.compile('([0-9]+)\D{,2}([0-9]+)')
        self._only_digits_regex = re.compile('[0-9]+')

        self._unit_converter = UnitConverter(Unit.FAHRENHEIT)

    def _get_additional_info(self, content):
        data = tuple(item.td.span.get_text()
                     for item in content.table.tbody.children)
        return data[:2]

    def _clear_str_number(self, str_number):
        result = self._only_digits_regex.match(str_number)
        return '--' if result is None else result.group()

    def _parse(self, container, criteria):
        results = [self._get_data(item, criteria)
                   for item in container.children]

        return [result for result in results if result]

    def _get_data(self, container, search_items):
        scraped_data = {}

        for key, value in search_items.items():
            result = container.find(value, class_=key)

            data = None if result is None else result.get_text()

            if data is not None:
                scraped_data[key] = data

        return scraped_data

    def _today_forecast(self, args):
        # raise NotImplementedError()
        criteria = {
            'today_nowcard-temp': 'div',
            'today_nowcard-phrase': 'div',
            'today_nowcard-hilo': 'div',
        }

        content = self._request.fetch_data(args.forecast_option.value,
                                           args.area_code)

        bs = BeautifulSoup(content, 'html.parser')

        container = bs.find('section', class_='today_nowcard-container')

        weather_conditions = self._parse(container, criteria)

        if len(weather_conditions) < 1:
            raise Exception('Could not parse weather forecast for today.')

        weatherinfo = weather_conditions[0]

        temp_regex = re.compile(('H\s+([0-9]+|\-{,2}).+'
                                 'L\s+([0-9]+|\-{,2})'))
        temp_info = temp_regex.search(weatherinfo['today_nowcard-hilo'])
        high_temp, low_temp = temp_info.groups()

        side = container.find('div', class_='today_nowcard-sidecar')
        wind, humidity = self._get_additional_info(side)

        curr_temp = self._clear_str_number(weatherinfo['today_nowcard-temp'])

        self._unit_converter.dest_unit = args.unit

        td_forecast = Forecast(self._unit_converter.convert(curr_temp),
                               humidity,
                               wind,
                               high_temp=self._unit_converter.convert(
                                   high_temp),
                               low_temp=self._unit_converter.convert(
                                   low_temp),
                               description=weatherinfo['today_nowcard-phrase'])

        return [td_forecast]

    def _five_and_ten_days_forecast(self, args):
        raise NotImplementedError()

    def _weekend_forecast(self, args):
        raise NotImplementedError()

    def run(self, args):
        self._forecast_type = args.forecast_option
        forecast_function = self._forecast[args.forecast_option]
        return forecast_function(args)

Looking at the call stack, it seems to me that it is something wrong in the Request class in weatherterm/core/request.py. 看一下调用堆栈,在我看来,weatherterm / core / request.py中的Request类有问题。 The initializer of the Request class should look like this: Request类的初始化程序应如下所示:

class Request:
    def __init__(self, base_url):
        ...

Make sure that the initializer also gets a base_url parameter. 确保初始化程序还获取了base_url参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM