Typeerror object takes no parameters python

scrapy TypeError: object() takes no parameters

I am new to Scrapy and trying to crawl a couple of links as a test using Scrapy. Whenever I run scrapy crawl tier1 , I get «TypeError: object() takes no parameters» as the following:

Traceback (most recent call last): File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/twisted/internet/defer.py", line 653, in _runCallbacks current.result = callback(current.result, *args, **kw) File "/Users/btaek/TaeksProgramming/adv/crawler/adv_crawler/adv_crawler/spiders/tier1_crawler.py", line 93, in parse mk_loader.add_xpath('title', 'h1[@class="top_title"]') # Title of the article File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/__init__.py", line 167, in add_xpath self.add_value(field_name, values, *processors, **kw) File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/__init__.py", line 77, in add_value self._add_value(field_name, value) File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/__init__.py", line 91, in _add_value processed_value = self._process_input_value(field_name, value) File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/__init__.py", line 150, in _process_input_value return proc(value) File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/processors.py", line 28, in __call__ next_values += arg_to_iter(func(v)) TypeError: object() takes no parameters 2017-08-23 17:25:02 [tier1-parse-logger] INFO: Entered the parse function to parse and index: http://news.mk.co.kr/newsRead.php?sc=30000001&year=2017&no=535166 2017-08-23 17:25:02 [tier1-parse-logger] ERROR: Error (object() takes no parameters) when trying to parse > from a mk article: http://news.mk.co.kr/newsRead.php?sc=30000001&year=2017&no=535166 2017-08-23 17:25:02 [tier1-parse-logger] ERROR: Error (object() takes no parameters) when trying to parse > from a mk article: http://news.mk.co.kr/newsRead.php?sc=30000001&year=2017&no=535166 2017-08-23 17:25:02 [scrapy.core.scraper] ERROR: Spider error processing (referer: None) Traceback (most recent call last): File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/twisted/internet/defer.py", line 653, in _runCallbacks current.result = callback(current.result, *args, **kw) File "/Users/btaek/TaeksProgramming/adv/crawler/adv_crawler/adv_crawler/spiders/tier1_crawler.py", line 93, in parse mk_loader.add_xpath('title', 'h1[@class="top_title"]') # Title of the article File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/__init__.py", line 167, in add_xpath self.add_value(field_name, values, *processors, **kw) File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/__init__.py", line 77, in add_value self._add_value(field_name, value) File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/__init__.py", line 91, in _add_value processed_value = self._process_input_value(field_name, value) File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/__init__.py", line 150, in _process_input_value return proc(value) File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/processors.py", line 28, in __call__ next_values += arg_to_iter(func(v)) TypeError: object() takes no parameters 
# -*- coding: utf-8 -*- import sys reload(sys) sys.setdefaultencoding('utf-8') import os sys.path.append(os.path.abspath('..')) import logging import scrapy from scrapy.loader import ItemLoader from adv_crawler.items import AdvCrawlerItem from datetime import datetime, date, time t1_parse_logger = logging.getLogger("tier1-parse-logger") t1_parse_logger.LOG_FILE = "Tier1-log.txt" content_type_dic = < 'news': 'news', >class Tier1Crawler(scrapy.Spider): name = "tier1" def start_requests(self): urls = ['http://news.mk.co.kr/newsRead.php?sc=30000001&year=2017&no=535982', 'http://news.mk.co.kr/newsRead.php?sc=30000001&year=2017&no=535166', ] for url in urls: yield scrapy.Request(url=url, callback=self.parse) def parse(self, response): t1_parse_logger.info("Entered the parse function to parse and index: %s" % response.url) # Log at the beginning of the parse function item_loader = ItemLoader(item=AdvCrawlerItem(), response=response) if 'mk.co.kr' in response.url: mk_loader = item_loader.nested_xpath('//div[@id="top_header"]/div[@class="news_title"]/div[@class="news_title_text"]') try: mk_loader.add_xpath('date', 'div[@class="news_title_author"]/ul/li[@class="lasttime"]') except AttributeError: # if the date is not in "lasttime" li tag mk_loader.add_xpath('date', 'div[@class="news_title_author"]/ul/li[@class="lasttime1"]') except Exception as e: # in case the error is not AttributeError t1_parse_logger.error("Error "+"("+str(e)+")"+" when trying to parse > from a mk article: %s" % response.url) try: mk_loader.add_xpath('author', 'div[@class="news_title_author"]/ul/li[@class="author"]') except AttributeError: # in case there is no author (some mk articles have no author) item_loader.add_value('author', "None") # ir error, replace with the line below # item['author'] = "None" # if the above gives any error, replace the above with this line except Exception as e: # in case the error is not AttributeError t1_parse_logger.error("Error "+"("+str(e)+")"+" when trying to parse > from a mk article: %s" % response.url) item_loader.add_xpath('content', '//div[@id="Content"]/div[@class="left_content"]/div[@id="article_body"]/div[@class="art_txt"]') # Content of the article (entire contents) mk_loader.add_xpath('title', 'h1[@class="top_title"]') # Title of the article item_loader.add_value('content_type', content_type_dic['news']) item_loader.add_value('timestamp', str(datetime.now())) # timestamp of when the document is being indexed item_loader.add_value('url', response.url) # url of the article t1_parse_logger.info("Parsed and indexed: %s" % response.url) return item_loader.load_item() 
# -*- coding: utf-8 -*- import scrapy from scrapy.loader.processors import Join, MapCompose, TakeFirst from w3lib.html import remove_tags def filter_date(value): if isinstance(value, unicode): (year, month, day) = str(value.split(" ")[-2]).split(".") return year+"-"+month+"-"+day def filter_utf(value): if isinstance(value, unicode): return value.encode('utf-8') class AdvCrawlerItem(scrapy.Item): author = scrapy.Field(input_processor=MapCompose(remove_tags, TakeFirst, filter_utf),) # Name of the publisher/author content = scrapy.Field(input_processor=MapCompose(remove_tags, Join, filter_utf),) # Content of the article (entire contents) content_type = scrapy.Field() date = scrapy.Field(input_processor=MapCompose(remove_tags, TakeFirst, filter_date),) timestamp = scrapy.Field() # timestamp of when the document is being indexed title = scrapy.Field(input_processor=MapCompose(remove_tags, TakeFirst, filter_utf),) # title of the article url = scrapy.Field() # url of the article 
import json from scrapy import signals from scrapy.exporters import JsonLinesItemExporter class AdvCrawlerJsonExportPipeline(object): def open_spider(self, spider): self.file = open('crawled-articles1.txt', 'w') def close_spider(self, spider): self.file.close() def process_item(self, item, spider): line = json.dummps(dict(item)) + "\n" self.file.write(line) return item 

I am aware that «TypeError: object() takes no parameters» error is usually thrown when __init__ method of a class is not defined at all or not defined to take in parameter(s). However, in the case above, how can i fix the error? Am I doing something wrong using the item loader or nested item loader??

Читайте также:  Echo printf and printf in php

Источник

«TypeError: object() takes no parameters» With python2 metaclass converted to python3

I’m converting some code from python2 to python3 and I’m hitting an error with a metaclass. This is the working python2 code (simplified):

#!/usr/bin/env python2 # test2.py class Meta(type): def __new__(mcs, name, bases, clsdict): new_class = type.__new__(mcs, name, bases, clsdict) return new_class class Root(object): __metaclass__ = Meta def __init__(self, value=None): self.value = value super(Root, self).__init__() class Sub(Root): def __init__(self, value=None): super(Sub, self).__init__(value=value) def __new__(cls, value=None): super(Sub, cls).__new__(cls, value) if __name__ == '__main__': sub = Sub(1) 
#!/usr/bin/env python3 # test3.py class Meta(type): def __new__(mcs, name, bases, clsdict): new_class = type.__new__(mcs, name, bases, clsdict) return new_class class Root(object, metaclass=Meta): def __init__(self, value=None): self.value = value super(Root, self).__init__() class Sub(Root): def __init__(self, value=None): super(Sub, self).__init__(value=value) def __new__(cls, value=None): super(Sub, cls).__new__(cls, value) if __name__ == '__main__': sub = Sub(1) 
Traceback (most recent call last): File "test.py", line 21, in sub = Sub(1) File "test.py", line 18, in __new__ super(Sub, cls).__new__(cls, value) TypeError: object() takes no parameters 

This isn’t a duplicate of the linked question because in that one the asker wasn’t invoking a simple class correctly. In this one I have code which worked in python 2 and doesn’t work with 2to3 run on it

You would have seen a warning about this in Python 2, except that DeprecationWarnings are suppressed by default. Here’s the long-ass comment about why object.__init__ and object.__new__ handle extra arguments in the weird way they do.

@Pynchia no reason other than the fact that it hasn’t been changed yet. AFAIK it doesn’t change behavior vs not declaring a parent in python 3

1 Answer 1

As described in depth by a comment in the Python 2 source code (as linked by user2357112 in a comment), Python considers it an error if you pass arguments to either object.__new__ or object.__init__ when both __init__ and __new__ have been overridden. If you override just one of those functions, the other one will ignore excess arguments, but if you override them both you’re supposed to make sure you only pass on arguments that are appropriate.

Читайте также:  Php загрузка excel файла

In this case, your Root class overrides __init__ but not __new__ , so the extra argument that gets passed to the inherited object.__new__ when you create an instance are ignored.

However, in Sub , you’re overriding both functions, and Sub.__new__ passes the parameter value on to object.__new__ in its super call. This is where you get an exception.

It’s technically an error in Python 2 as well as Python 3, but the Python developers decided that raising an exception in that situation would cause too much old code to break, so Python 2 only issues a warning (which is suppressed by default). Python 3 breaks backwards compatibility in several other ways, so breaking old code for this issue as well is not as big a deal.

Anyway, the proper way to fix your code is either to add a __new__ method to Root that accepts and suppresses the value argument (e.g. it doesn’t pass it on to object.__new__ ), or to change Sub so that it doesn’t pass the value to its parent at all (e.g. it just calls super(Sub, cls).__new__(cls) ). You might want to think a bit about whether you actually need both __new__ and __init__ methods in Sub , since most classes only need to override one of them.

Источник

Python: object.__new__() takes no parameters [duplicate]

Right now I’m working on a program that allows people to make tests, save them to databases, and then print them. I keep getting the error:

Traceback (most recent call last): File "C:/Users/Shepard/Desktop/Gradebook.py", line 50, in qs = QuestionStorage("questions.db") TypeError: object.__new__() takes no parameters 

Anyone have any idea? I’m assuming its somewhere in the QuestionStorage class, but I’m not quite able to figure anything out. This is my first time working with SQLite3, and I’m having quite a bit of trouble, if anyone can help me, that would be great. 🙂

import sqlite3 class QuestionStorage(object): def _init_(self, path): self.connection = sqlite3.connect(path) self.cursor = self.connection.cursor() def Close(self): self.cursor.close() self.connection.close() def CreateDb(self): query = """CREATE TABLE questions (id INTEGER PRIMARY KEY, Question TEXT, Answer1 TEXT, Answer2 TEXT, Answer3 TEXT, Answer4 TEXT, CorrectAnswer TEXT)""" self.cursor.exeute(query) self.connection.commit() #self.cursor.close() def AddQuestion(self, Question, Answer1, Answer2, Answer3, Answer4): self.cursor.execute("""INSERT INTO questions VALUES (?, ?, ?, ?, ?, ?)""", (None, Question, Answer1, Answer2, Answer3, Answer4, CorrectAnswer)) self.connection.commit() def GetQuestion(self, index = None): self.cursor.execute("""SELECT * FROM questions WEHRE (index,)) res = self.cursor.fetchone() return res print ("TestMaker v.1") print ("To create a multiple choice test, follow the directions.") testName = input ("Give your test a name.") testQ = int(input ("How many questions will be on this test? (Numeric value only.)")) counter = 1 while counter  

Источник

Оцените статью