【问题标题】:how to fetch data from google plus using api key in python?如何在 python 中使用 api 密钥从 google plus 获取数据?
【发布时间】:2013-09-23 10:18:50
【问题描述】:

**当我发送请求时——

f = urllib.urlopen(https://www.googleapis.com/plus/v1/people/103777531434977807649/activities/public?key=*************** )
json=f.read()
print json

它返回类似这样的东西,而不是所需的 json

{
"kind": "plus#activityFeed",
 "etag": "\"seVFOlIgH91k2i-GrbizYfaw_AM/chWYjTdvKRLG9yxkeAfrCrofGHk\"",
 "nextPageToken": "CAIQ__________9_IAAoAA",
 "title": "Google+ List of Activities for Collection PUBLIC",
 "items": []
}

我必须做些什么才能得到正确的回应????

这是代码: 导入json

    f = urllib.urlopen('https://www.googleapis.com/plus/v1/people/'+id+'/activities    /public?key=*****************&maxResults=100')               
    s = f.read()                            
    f.close()              
    ss=json.loads(s)
            print ss 
            try:        
        nextpagetoken=str(ss['nextPageToken'])
        i=0
        str_current_datetime=str(datetime.now())
        gp_crawldate=str_current_datetime.split(" ")[0]
        gp_groupid=id
        db = MySQLdb.connect("localhost","root","****","googleplus" )
        cursor=db.cursor()

        while i<len(ss['items']):

            gp_kind=str(ss['items'][i]['kind'])
            gp_title=str(ss['items'][i]['title'].encode('utf8'))
            gp_published=str(ss['items'][i]['published'][0:10])
                            check=int(cool(str(ss['items'][i]['published'][0:19])))#this method is defined in the code 
            gp_activityid=str(ss['items'][i]['id'])
            gp_actorid=str(ss['items'][i]['actor']['id'])
            gp_verb=str(ss['items'][i]['verb'])
            gp_objecttype=str(ss['items'][i]['object']['objectType'])
            gp_originalcontent=str(ss['items'][i]['object']['content'].encode('utf8'))  
            gp_totalreplies=str(ss['items'][i]['object']['replies']['totalItems'])
            gp_totalplusone=str(ss['items'][i]['object']['plusoners']['totalItems'])
            gp_totalreshare=str(ss['items'][i]['object']['resharers']['totalItems'])
            #gp_geocode=str(ss['items'][i]['geocode'])
            #gp_placename=str(ss['items'][i]['placeName'])
            i=i+1

g+api有什么变化???

【问题讨论】:

  • import json... 然后使用page = f.read()json_data = json.loads(page) 或类似的...它不会自动发生...您可能希望查看requests 模块,它使访问 HTTP 资源可能会更好
  • 你刚刚告诉我的事情我已经完成了。但它返回相同的响应。当我通过浏览器发送 http 请求时,重新调整了相同的 json
  • 在这种情况下,您是否考虑将您正在使用的代码(您已经完成)并将其作为edit 放到您的问题中
  • 我已经添加了代码 jon

标签: python json python-2.7 google-api google-plus


【解决方案1】:

您发布的回复是正确的回复。如果items 字段是一个空列表,那么您为其获取帖子的用户可能从未公开发布过任何内容。在这种情况下,我仅通过访问用户的个人资料即可确认该用户没有公开帖子。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多