【问题标题】:Search has no attribute teaser搜索没有属性预告片
【发布时间】:2014-08-01 15:45:26
【问题描述】:

我正在尝试访问teaser。我尝试了很多不同的东西,例如“print(search.teaser)”,你们知道我必须做什么才能访问teaser吗?

import re
import json
import requests


class search:
    def run():

        data = requests.get("http://boards.4chan.org/g/catalog").text
        match = re.match(".*var catalog = (?P<catalog>\{.*\});.*", data)

        if not match:
            print("Couldn't scrape catalog")
            exit(1)

        catalog = json.loads(match.group('catalog'))

        running = True
        while running:

                try:
                     filtertext = ("tox")
                     for number, thread in catalog['threads'].items():
                        sub, teaser = thread['sub'], thread['teaser']
                        if filtertext in sub.lower() or filtertext in teaser.lower():

                        return(teaser)
                        running = False


            except KeyboardInterrupt:
                running = False


print(search.teaser)

【问题讨论】:

  • 你认为预告片是从哪里来的?应该是def run(self):,但你根本不需要上课。
  • 你永远不会分配self.teaser,它只是search.run中的一个局部变量。我建议你看看docs.python.org/2/tutorial/classes.html,或者只是使用一个函数。

标签: python regex json python-3.x


【解决方案1】:

不完全确定您要做什么,但我认为您想调用方法run。 您的Search 类没有attribute teaser,您在run 方法中将teaser 定义为您在该方法中返回的变量:

class Search:
    def run(self): # need the self parameter

        data = requests.get("http://boards.4chan.org/g/catalog").text
        match = re.match(".*var catalog = (?P<catalog>\{.*\});.*", data)

        if not match:
            print("Couldn't scrape catalog")
            exit(1)  
        catalog = json.loads(match.group('catalog'))   
        running = True
        while running:    
            try:
                filtertext = ("tox")
                for number, thread in catalog['threads'].items():
                    sub, teaser = thread['sub'], thread['teaser']
                    if filtertext in sub.lower() or filtertext in teaser.lower():
                        return teaser  # return the value of the variable teaser defined above
                running = False    
            except KeyboardInterrupt:
                running = False


s = Search() # create instance
print (s.run()) # call run method

哪些输出:

 Tox is a secure, distributed multimedia messenger aimed at simplifying encrypted communications by means of simple interfaces, no registration, and a wide array of supported platforms. --- Venom now has apt-get install-ability --- wget https://repo.tox.im/tox-apt.sh &amp;&amp; sudo chmod +x ./tox-apt.sh &amp;&amp; ./tox-apt.sh then, apt-get install venom You can update venom through apt-get everytime there is a new successful build for Venom. Clients such as uTox and qTox are in the works for an apt repository, as they currently are not packaged. Venom already ships as .deb, and thus is already ready to ship. https://tox.im https://wiki.tox.im https://github.com/Tox https://github.com/irungentoo/toxcore Want to Groupchat? Add syncbot@toxme.se on your favorite Tox application.

一个使用属性的例子:

class Foo():
    def __init__(self):
        self.name = "" # attribute
        self.age = "" # attribute
f = Foo()
f.name = "Foobar" # access attribute and set to "Foobar"
f.age = 34   # access attribute and set to 34
print(f.name,f.age) # print updated attribtue values
Foobar 34

【讨论】:

  • 谢谢,我会试试这个。
猜你喜欢
  • 1970-01-01
  • 2021-06-20
  • 1970-01-01
  • 2021-11-25
  • 1970-01-01
  • 2016-03-19
  • 1970-01-01
  • 2018-08-20
  • 2022-08-19
相关资源
最近更新 更多