我应该如何重新思考这里的对象/对象方向?
我(主要是想学习 python 和 json,但也)尝试定期从 Twitter 上提取和格式化热门主题列表。我浏览了很多不同的教程,将其拼凑在一起。它满足了我的目的——将我需要的 HTML 打印到标准输出,但我想知道我是否可以以不同的方式思考对象或更好地构建它。协助?
class trend:
#these are the fields that Twitter provides, so they make up one trend.
def __init__(self, query, name, promoted_content, events, url):
self.query = query
self.name = name
self.promoted_content = promoted_content
self.events = events
self.url = url
def listitem(self):
print "\t <li><a href=\"%s\">%s</a></li>\n" %(self.url, self.name)
class trending:
def __init__(self,api_url,title):
self.api_url = api_url
self.title = title
def get_trending(self):
import simplejson as json
import urllib2
trends_all = json.loads(urllib2.urlopen(self.api_url).read())
# test print
# print trends_all[0]['trends']
print "<p>%s</p> \n <ol>" % self.title
#I'm initializing an array, though I don't actually use it. That's next.
trends = []
for x in trends_all[0]['trends']:
thistrend = trend(x['query'], x['name'], x['promoted_content'], x['events'], x['url'])
thistrend.listitem()
trends.append(thistrend)
print "</ol>\n"
return trends
usa = trending("http://api.twitter.com/1/trends/23424977.json","Trending nationally")
usa.get_trending()
反馈?
I'm (mostly trying to learn python and json, but also) trying to periodically pull and format a list of trending topics off of twitter. I cobbled this together skimming a lot of different tutorials. It serves my purposes -- prints the HTML I need to stdout, but I'm wondering if I could have thought about objects differently or structured it better. Assist?
class trend:
#these are the fields that Twitter provides, so they make up one trend.
def __init__(self, query, name, promoted_content, events, url):
self.query = query
self.name = name
self.promoted_content = promoted_content
self.events = events
self.url = url
def listitem(self):
print "\t <li><a href=\"%s\">%s</a></li>\n" %(self.url, self.name)
class trending:
def __init__(self,api_url,title):
self.api_url = api_url
self.title = title
def get_trending(self):
import simplejson as json
import urllib2
trends_all = json.loads(urllib2.urlopen(self.api_url).read())
# test print
# print trends_all[0]['trends']
print "<p>%s</p> \n <ol>" % self.title
#I'm initializing an array, though I don't actually use it. That's next.
trends = []
for x in trends_all[0]['trends']:
thistrend = trend(x['query'], x['name'], x['promoted_content'], x['events'], x['url'])
thistrend.listitem()
trends.append(thistrend)
print "</ol>\n"
return trends
usa = trending("http://api.twitter.com/1/trends/23424977.json","Trending nationally")
usa.get_trending()
feedback?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在你的例子中,我不明白为什么趋势需要成为一个类,因为它只有一个功能。这可以使用
get_trending
作为独立函数编写,该函数将 api_url 和标题作为参数。In your example, I don't see why trending needs to be a clas at all, since it only has one function. This could be written with
get_trending
as a standalone function, which takes the api_url and title as arguments.