【发布时间】: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