搜索没有属性预告片

时间:2014-08-01 15:45:27

标签: python regex json python-3.x

我正在尝试访问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)

1 个答案:

答案 0 :(得分:2)

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

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