作为标题我需要从appstore获得评论和费率,日期。 我从GitHub获得了github.com/don9z/appstore
这是代码:
import feedparser
from datetime import datetime
import calendar
app_contries = {
'China':'cn',
'United States':'us'}
app_sortby = {
'Most Recent':'mostRecent',
'Most Helpful':'mostHelpful',
# 'Most Favorable':'mostFavorable',
# 'Most Critical':'mostCritical'}
def mk_appfeed(app_id, country_code, sortby=app_sortby['Most Recent']):
return 'https://itunes.apple.com/%s/rss/customerreviews/id=%s/sortBy=%s/xml' %\
(country_code,app_id, sortby)
def parse_feed(url):
review_map = {}
feed = feedparser.parse(url)
review_map.setdefault('title', feed.feed['title'])
review_map.setdefault('updated',
calendar.timegm(feed.feed['updated_parsed']))
if not feed.entries:
print 'Get nothing from ' + url
return None
review_map.setdefault('apptitle', feed.entries[0]['title'])
review_map.setdefault('applink', feed.entries[0]['id'])
reviews = []
for entry in feed.entries[1:]:
reviews.append({'title': entry['title'],
'content': entry['content'][0]['value'],
'author': entry['author'],
'rating': entry['im_rating'],
'updated': calendar.timegm(entry['updated_parsed'])
})
review_map.setdefault('reviews', reviews)
return review_map
def print_reviews(review_map):
if not review_map:
print 'Input None'
return
print 'Title: %s' % review_map['title']
print 'Updated: %s' % datetime.fromtimestamp(review_map['updated'])
print 'App Name: %s' % review_map['apptitle']
print 'Link: %s' % review_map['applink']
index = 0
for entry in review_map['reviews']:
index+=1
print '*** %d ***' % index
print 'Title: %s' % entry['title']
print 'Content: %s' % entry['content']
print 'Author: %s' % entry['author']
print 'Rating: %s' % entry['rating']
print 'Updated %s' % datetime.fromtimestamp(entry['updated'])
def get_app_reviews(app_id, country_name, sortby='Most Recent'):
print_reviews(parse_feed(mk_appfeed(app_id, app_contries[country_name],
app_sortby[sortby])))
我只使用call.py
import review
review.get_app_reviews('506627515', 'United States')
但命令返回
Title: iTunes Store: Customer Reviews
Traceback (most recent call last):
File "call.py", line 3, in <module>
review.get_app_reviews('506627515', 'United States')
File "/Users/Eric/Desktop/appstore-master/review.py", line 71, in get_app_reviews
app_sortby[sortby])))
File "/Users/Eric/Desktop/appstore-master/review.py", line 54, in print_reviews
print 'Updated: %s' % datetime.fromtimestamp(review_map['updated'])
AttributeError: 'module' object has no attribute 'fromtimestamp'
似乎日期时间有问题 我只想找出问题所在 顺便说一句,我的python是2.7.0,我认为是版本问题?
需要帮助
答案 0 :(得分:0)
因此,在datetime
中,fromtimestamp
命令位于date
模块中,参见docs。因此,将代码更改为
import datetime
print 'Updated: %s' % datetime.date.fromtimestamp(review_map['updated'])
请注意review_map
中的项目由calendar.timegm()
创建,它返回一个Unix时间戳,参见docs。
仅出于说明的目的,完整的程序就像是
datetime.date.fromtimestamp(calendar.timegm(time.gmtime()))
返回datetime.date(2017,7,12)
如果您的entry
对象返回正确的格式,这应该可行。