Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
';str';对象在python类函数中不可调用_Python_String_Class - Fatal编程技术网

';str';对象在python类函数中不可调用

';str';对象在python类函数中不可调用,python,string,class,Python,String,Class,我正在构建一个webscraper,它有一个页面类。但是,将属性url分配给页面类时,我会收到以下错误消息 TypeError:“str”对象不可调用 在下面找到我的代码: class Page: def __init__(self, pid, cur_id, url=None, proxy=None): self.pid = pid self.cur_id = cur_id if url is None: self

我正在构建一个webscraper,它有一个
页面类
。但是,将属性
url
分配给
页面类时,我会收到以下错误消息

TypeError:“str”对象不可调用

在下面找到我的代码:

class Page:
    def __init__(self, pid, cur_id, url=None, proxy=None):
        self.pid = pid
        self.cur_id = cur_id
        if url is None:
            self.url = self._build_url()
        else:
            self.url = url
        self.content = get_page_content(self.url, proxy)
        self.crawl_date = datetime.now()

    @property
    def _build_url(self):
        my_url = root + self.pid
        return my_url
有什么建议吗

编辑: 完全回溯:

> Traceback (most recent call last):   File "C:\Program
> Files\JetBrains\PyCharm Community Edition
> 2019.1.3\helpers\pydev\pydevd.py", line 2060, in <module>
>     main()   File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.3\helpers\pydev\pydevd.py", line 2054, in main
>     globals = debugger.run(setup['file'], None, None, is_module)   File "C:\Program Files\JetBrains\PyCharm Community Edition
> 2019.1.3\helpers\pydev\pydevd.py", line 1405, in run
>     return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)   File "C:\Program Files\JetBrains\PyCharm Community
> Edition 2019.1.3\helpers\pydev\pydevd.py", line 1412, in _exec
>     pydev_imports.execfile(file, globals, locals)  # execute the script   File "C:\Program Files\JetBrains\PyCharm Community Edition
> 2019.1.3\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
>     exec(compile(contents+"\n", file, 'exec'), glob, loc)   File "C:/single_offers.py",
> line 146, in <module>
>     main()   File "C:/single_offers.py",
> line 68, in main
>     p = Page(id, cur_id, proxy=proxy)   File "C:/single_offers.py",
> line 109, in __init__
>     self.url = self._build_url() TypeError: 'str' object is not callable
> 
> Process finished with exit code 1
>回溯(最后一次调用):文件“C:\Program”
>文件\JetBrains\PyCharm社区版
>2019.1.3\helpers\pydev\pydev.py“,第2060行,在
>main()文件“C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.3\helpers\pydev\pydevd.py”,第2054行,在main中
>globals=debugger.run(setup['file'],None,None,is_module)文件“C:\Program Files\JetBrains\PyCharm Community Edition
>2019.1.3\helpers\pydev\pydev.py“,第1405行,运行中
>返回self.\u exec(是模块、入口点、模块名称、文件、全局、局部)文件“C:\Program Files\JetBrains\PyCharm社区
>2019.1.3版\helpers\pydev\pydevd.py“,第1412行,在_exec中
>pydev_imports.execfile(文件、全局、局部)#执行脚本文件“C:\Program Files\JetBrains\PyCharm Community Edition”
>2019.1.3\helpers\pydev\\u pydev\u imps\\u pydev\u execfile.py”,execfile中第18行
>exec(compile(contents+“\n”,file,'exec'),glob,loc)file“C:/single\u offers.py”,
>第146行,在
>main()文件“C:/single\u offers.py”,
>第68行,主
>p=页面(id,cur\u id,proxy=proxy)文件“C:/single\u offers.py”,
>第109行,in_uuuinit__
>self.url=self.\u build\u url()类型错误:“str”对象不可调用
> 
>进程已完成,退出代码为1

您已将
\u build\u url
声明为属性。因此,您只需使用属性名访问它,而不是通过调用它:

self.url = self._build_url

您已将
\u build\u url
声明为属性。因此,您只需使用属性名访问它,而不是通过调用它:

self.url = self._build_url

您将
\u build\u url
设置为属性,因此
self。\u build\u url
在没有调用参数的情况下获取
str
,但您添加了参数以使其成为
self.url=self。\u build\u url()
。所以您试图调用结果字符串。要么删除
@属性
装饰器,使其成为一个普通方法,要么删除使用点的调用参数,以便在不调用属性的情况下使用该属性。

您将
\u build\u url
设置为属性,因此
self.\u build\u url
获取不带调用参数的
str
,但是您添加了参数使其
self.url=self.\u build\u url()
。所以您试图调用结果字符串。请删除
@属性
装饰器,使其成为普通方法,或者删除使用点的调用参数,以便在不调用属性的情况下使用该属性。

您创建了
\u url
一个
@属性
。因此,当您要调用此方法时,不应使用
self.\u build\u url()
,而应使用
self.\u build\u url
(不带括号)。

您创建了
\u build\u url
属性。因此,当您要调用此方法时,不应使用
self.\u build\u url()
,而应使用
self.\u build\u url
(不带括号)。

您可以提供完整的错误堆栈跟踪,而不仅仅是最后一行吗?您可以提供完整的错误堆栈跟踪,而不仅仅是最后一行吗?