简体   繁体   English

用方法调用python3类

[英]call python3 class with methods

I have a file named: scrape.py This file contains the following class: 我有一个名为:scrape.py的文件,该文件包含以下类:

Im trying to call my class BrickSetSpider , with its methods, and ultimately return the value of parse_page_urls when running this program like python scrape.py . 我试图调用我的类BrickSetSpider及其方法,并最终在运行此程序(如python scrape.py时返回parse_page_urls的值。

However, when i run python scrape.py , I get: 但是,当我运行python scrape.py ,我得到:

NameError: name 'response' is not defined NameError:未定义名称“响应”

on this line: 在这条线上:

page = spider.parse_se_page_url(response)

Can someone help me run my two methods (outputting the result of g_result_list ? 有人可以帮我运行我的两个方法(输出g_result_list的结果吗?

Thank you so much! 非常感谢!

A few thins: 一些薄:

  • The reason you get the error of NameError: name 'response' is not defined is that no where in the scope of main you have defined any variable called response You can't use it without defining such a parameter. 出现NameError: name 'response' is not defined错误的原因NameError: name 'response' is not defined因为在main范围内没有定义任何名为response变量的地方。如果没有定义这样的参数,就无法使用它。
  • In addition in the parse_se_page_urls you have an extra line of 此外,在parse_se_page_urls您还有一行

     def parse_se_page_urls(self, g_result_page): 

    without any implementation of the function. 没有该功能的任何实现。 should also be problematic. 也应该是有问题的。

  • Unless missing something that isn't in the code posted, in your second function you are using the g_result_page variable - but just as with response it is not defined. 除非缺少所发布代码中未包含的内容,否则在第二个函数中,您将使用g_result_page变量-但就像未定义响应一样。 You have a parameter with the same name in the function above but it is a local variable of that function and thus unknown by the second one. 您在上面的函数中有一个具有相同名称的参数,但是它是该函数的局部变量,因此第二个未知。 To initialize them as members of the instance add a constructor as follows: 要将它们初始化为实例成员,请添加如下构造函数:

     def __init__(self): self.g_result_list = [] self.g_result_page = [] 

    and then for using write self.g_result_page 然后用于写self.g_result_page

  • In your first function you have the two following lines: 在第一个函数中,有以下两行:

     set(g_result_page) list(g_result_page) 

    These lines have no effect - they create a set and a list out of g_result_page but these data structures are not assigned to anything and thus are cleared at the end of the function 这些行无效-它们在g_result_page创建了一个set和一个list ,但是这些数据结构没有分配任何内容,因此在函数末尾被清除

There are two minor errors in your code. 您的代码中有两个小错误。

First, when you get an instance of the class, you don't need to pass in anything, so your code becomes 首先,当您获得该类的实例时,您无需传递任何内容,因此您的代码变为

spider = BrickSetSpider()

Secondly, you use the variable page to store the output of spider.parse_page_url(response) by doing page = spider.parse_page_url(response) , but you then call urls = page.parse_page_urls(result_page) . 其次,通过执行page = spider.parse_page_url(response)来使用变量page来存储spider.parse_page_url(response)的输出,但是您随后调用urls = page.parse_page_urls(result_page) I believe what you meant to do was call urls = spider.parse_page_urls(page) . 我相信您的意思是致电urls = spider.parse_page_urls(page)

Hope this helps. 希望这可以帮助。

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

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