Python 搜索没有属性摘要
我正在尝试访问Python 搜索没有属性摘要,python,regex,json,python-3.x,Python,Regex,Json,Python 3.x,我正在尝试访问摘要。我尝试了很多不同的方法,比如“print(search.triser)”,“你们知道我必须做什么才能访问triser 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 =
摘要
。我尝试了很多不同的方法,比如“print(search.triser)”,“你们知道我必须做什么才能访问triser
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)
重新导入
导入json
导入请求
类别搜索:
def run():
数据=请求。获取(“http://boards.4chan.org/g/catalog)。文本
match=re.match(“.*var catalog=(?P\{.*\});*”,数据)
如果不匹配:
打印(“无法刮取目录”)
出口(1)
catalog=json.load(match.group('catalog'))
运行=真
运行时:
尝试:
filtertext=(“tox”)
对于编号,目录['threads']中的线程。items()
子线程,梳理器=线程['sub'],线程['striser']
如果sub.lower()中的filtertext或streamer.lower()中的filtertext:
返回(摘要)
运行=错误
除键盘中断外:
运行=错误
打印(search.trister)
不太清楚您想做什么,但我认为您想调用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
使用属性的示例:
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
你认为挑逗者来自哪里?它应该是
def run(self):
但实际上你根本不需要类。你从不分配self.trister
,它只是search.run中的一个局部变量。我建议你看看,或者只是使用一个函数。谢谢,我会试试这个。
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