Python 获取Google+特定用户最新动态

本文介绍如何使用Python编程获取Google+特定用户的最新动态,涉及数据挖掘技术,通过示例代码展示整个爬取过程。
摘要由CSDN通过智能技术生成

CODE:

#!/usr/bin/python 
# -*- coding: utf-8 -*-

'''
Created on 2014-8-28
@author: guaguastd
@name: login.py

# Request over http
def google_login_http(resource_type, action, field):
    
    # import requests
    import requests
    import re
    
    # key information
    key = ''
    
    # base url
    url = 'https://www.googleapis.com/plus/v1'
    
    # build standard field
    for matStr,repStr in [[" ", "+"], ["'", "%27"]]:
        reobj = re.compile(matStr)
        field = reobj.sub(repStr, field)
        
    # Build the urls
    urls = '%s/%s?%s=%s&alt=json&key=%s' % (url, resource_type, action, field, key)
    #print 'urls: ', urls
    
    # Get the content
    content = requests.get(urls, verify=False).json()
    
    # return
    return content

# Build personal google_api
# sType: 0 means people; 1 means activities; 2 means comments; 3 means moments
def google_api_request(sType, **kwargs):
    # import requests
    import requests
    
    # key information
    key = ''
    
    # base url
    url = 'https://www.googleapis.com/plus/v1'
        
    # Build the request
    if sType == 0:
        pass
    elif sType == 1:
        action = kwargs['action']
        if action is 'list':
            collection = kwargs['collection']
            userId = kwargs['userId']
            if 'maxResults' in kwargs:
                maxResults = kwargs['maxResults']
            else:
                maxResults = 20
            urls = '%s/people/%s/activities/%s?maxResults=%s&key=%s' % (url, userId, collection, maxResults, key)
            content = requests.get(urls, verify=False).json()
            
    return content
                

#!/usr/bin/python 
# -*- coding: utf-8 -*-

'''
Created on 2014-9-4
@author: guaguastd
@name: user_activity_fetch.py
'''

import json
from login import google_api_request, google_login_http

while True:
    query = raw_input("Input query(None to quit): ")
    if query.strip() == '':
        break
    
    people_feed = google_login_http("people", "query", query)
    #people_feed = google_api.people().search(query=query).execute()
    #print json.dumps(people_feed['items'], indent=1)
    
    for user in people_feed['items']:
        userId = user['id']
        activity_feed = google_api_request(1, action='list', collection='public', maxResults='100', userId=userId)
        print json.dumps(activity_feed, indent=1)

RESULT:

    "content": "I read Brad Stone's history of Amazon, <i>The Everything Store</i>, \u00a0over the holidays, and recommend it highly. \u00a0To be sure, it is incomplete and doubtless has many inaccuracies, but it gives insight into the mind of a remarkable man and the company he has built - a company with profound influence on the present and future shape of our society.<br /><br />I had heard scuttlebutt that the book was hostile to Amazon. \u00a0I didn't find that to be the case at all. In fact, it increased my admiration for Jeff and what he and his team have accomplished.<br /><br />After Steve Jobs died, everyone was saying "Will we ever see his like again?" \u00a0I would always respond, "What do you mean? \u00a0He's already here, and his name is Jeff Bezos. \u00a0He's the only other tech entrepreneur I know who has transformed multiple industries, and shown the ability to work his magic not once but many times. \u00a0He's been the misunderstood underdog who came out on top because of vision, passion, and persistence."<br /><br />(OK, since then, Elon Musk has shown signs of pulling off the same ambition. But the fact remains that Jeff is one of the most important and successful entrepreneurs of our time.)<br /><br />Because of my admiration and liking for Jeff, \u00a0I was a bit dismayed to see the book position me as an "adversary." \u00a0While in some ways I am a competitor to Amazon, I think of myself more as a partner and friend than any kind of adversary! \u00a0And while Jeff and I have occasionally butted heads, first about the 1-click patent back in 2000 (a conflict that ended up with us as friends), and later about some of Amazon's overly aggressive business tactics towards suppliers (described in chapter 10 of the book), and about the use of a proprietary DRM'd ebook format rather than open standards for the Kindle, I have always been a huge fan.<br /><br />One of the things the book gets across is what a great <b>learner</b> Jeff is. It makes clear just how freshly he responded to the challenges of growing his business, relying on some uncompromising principles but also adapting them so that, as long-time Amazon employee Rick Dalzell described, he always engaged his decision-making around "the best truth at the time." (Chapter 9, page 267) His intense curiosity is one of the most striking things about him.\u00a0<br /><br />The book also underplays Jeff's humanity, humor, and kindness. There are a lot of stories of how forceful, even abrasive, he sometimes is with subordinates - and I imagine that can be unpleasant. \u00a0But I also know just how hard it is to get thousands of people moving in the same direction without ruffling any feathers. And some of the changes that Jeff had to make to the company direction required enormous determination and force. I wish that some other leaders I know (e.g. in government) had equal clarity and determination.<br /><br />The book also really helped me see how deep Jeff's focus on the customer is. While I have always believed that focus to be sincere, I have also always worried that it would fade as the company became dominant, as is so often the case. \u00a0But the book makes clear again and again how it really is a touchstone for Jeff.<br /><br />I have also worried that focus on the customer isn't enough - that companies that become as powerful as Amazon also need to understand the complete ecosystem in which they operate. \u00a0The book's account of Amazon's sometimes brutal interactions with companies that it wanted to acquire, like Quidsi, the company behind <a href=\"http://diapers.com\">diapers.com</a> (page 298), and suppliers like German knife-maker Wusthof (page 300 and ff) makes clear that Amazon hasn't fully learned that lesson, and seems to believe that as long as customers benefit, it's ok to hurt suppliers. Sometimes that is true, when suppliers are inefficient or exploitive of their customers, but in other cases, squeezing all the profit out of suppliers' businesses is enormously short sighted. \u00a0The ideal ecosystem is one where everyone flourishes, not where one company flourishes at the expense of all the others.<br /><br />But I got a lot of hope from reading about Jeff's "Amazon.love memo" (Chapter 10, page 317-318), in which he analyzed why some big and powerful companies are hated, while others continue to engender love from not only their customers but their entire ecosystem. \u00a0In particular, I liked that one of the principles that Jeff distilled was this one:<br /><br />"Capturing all the value only for the company is not cool."<br /><br />I've long urged companies to make "Create more value than you capture" their watchword, because it seems to me that building a healthy ecosystem in which everyone - employees, customers, suppliers, partners, and even competitors - can flourish is key to the positive impact that capitalism can have on society as a whole. \u00a0(See <a href=\"http://radar.oreilly.com/2009/01/work-on-stuff-that-matters-fir.html\">http://radar.oreilly.com/2009/01/work-on-stuff-that-matters-fir.html</a>)<br />The fact that Jeff is thinking about this as Amazon gets more dominant is a really good sign. \u00a0Some companies never realize that they need to be especially careful to create value for everyone as they get larger and more powerful.\u00a0<br /><br />My chief beef about the book is that it skimped on some of the really big management lessons from Amazon. For me one of the most fascinating things about Amazon is the way that it seems to have modeled its organizational culture on its software. Stories I've heard about the way small teams are organized at Amazon remind me of the way modern modular software is designed. \u00a0I suspect that Amazon has cracked the code of the problem that was once laid out to me by the CIO of Fidelity Investments: \u00a0"We know about all these new technologies. What we don't know is how to organize our company around them." \u00a0<br /><br />In addition, I wish there were more written about the design of Amazon's internal services. When Jeff had the insight that led to the rise of Amazon as a platform - not only that its software needed to make the transition from a single monolithic application to a series of reusable web services, but also that those services should be designed in such a way that they could be re-used as easily by developers outside the company as by its internal units - he demonstrated a lesson that has yet to be learned by most large organizations.<br /><br />Take the recent <a href=\"http://healthcare.gov\">healthcare.gov</a> debacle. \u00a0One of the functions that the system depends on is income verification by the IRS. That lookup is, consistent with monolithic old-school software development, a tightly integrated part of the application rather than a true reusable service. If <a href=\"http://healthcare.gov\">healthcare.gov</a> were designed with the lessons of Amazon in mind, not only could the Federal <a href=\"http://healthcare.gov\">healthcare.gov</a> site call IRS web services to do income verification, but so could any of the state healthcare exchanges, or, for that matter, any private insurance company - or any company that needed to do income verification for any purpose.<br /><br />I realize that this is a general business book, and getting too deep on the technology might have scared off many readers, but I do hope that some future book about Amazon will give more than tantalizing glimpses of the interplay between the software architecture of a large-scale web enterprise like Amazon and the human and organizational architecture \u00a0that makes it possible for that software to be deployed in as agile a way as possible.<br /><br />I highly recommend this book. Amazon is one of the most important companies in the 21st century economy, and anyone whose business has been or will be touched by Amazon should be sure to read it.\ufeff", 
    "plusoners": {
     "totalItems": 153, 
     "selfLink": "https://www.googleapis.com/plus/v1/activities/z12wtrkzern0enaad04chl3pvxfbszirt5o/people/plusoners"
    }, 
    "replies": {
     "totalItems": 7, 
     "selfLink": "https://www.googleapis.com/plus/v1/activities/z12wtrkzern0enaad04chl3pvxfbszirt5o/comments"
    }, 
    "objectType": "note"
   }, 
   "updated": "2014-01-07T22:28:19.761Z", 
   "actor": {
    "url": "https://plus.google.com/107033731246200681024", 
    "image": {
     "url": "https://lh4.googleusercontent.com/-J8nmMwIhpiA/AAAAAAAAAAI/AAAAAAACoLI/0MpUu2BMqTM/photo.jpg?sz=50"
    }, 
    "displayName": "Tim O'Reilly", 
    "id": "107033731246200681024"
   }, 
   "access": {
    "items": [
     {
      "type": "public"
     }
    ], 
    "kind": "plus#acl", 
    "description": "Public"
   }, 
   "verb": "post", 
   "etag": "\"L2Xbn8bDuSErT6QA3PEQiwYKQxM/TcfzI9F5YzoQZ5iYwx3udd2xYFQ\"", 
   "published": "2014-01-07T22:28:19.761Z", 
   "id": "z12wtrkzern0enaad04chl3pvxfbszirt5o"
  }, 
  {
   "kind": "plus#activity", 
   "provider": {
    "title": "Google+"
   }, 
   "title": "This piece about bringing humanity to government services is profound and important. Read it!", 
   "url": "https://plus.google.com/107033731246200681024/posts/4YiAQHd8Zzv", 
   "object": {
    "resharers": {
     "totalItems": 28, 
     "selfLink": "https://www.googleapis.com/plus/v1/activities/z12vwdj4gkzdtfakp23gcxizetybvpydh/people/resharers"
    }, 
    "attachments": [
     {
      "displayName": "People, Not Data", 
      "fullImage": {
       "url": "https://d262ilb51hltx0.cloudfront.net/max/800/1*rjIrCUCyf_503cNoAvxCGQ.png", 
       "type": "image/jpeg"
      }, 
      "url": "https://medium.com/p/47434acb50a8", 
      "image": {
       "url": "https://lh5.googleusercontent.com/proxy/TM_lWItfnkZ8_yhJvnO1jLdHS15NZqX48VPptcEpNuZ6carqTbubjk-Wd5lRWkbXWQg6L6t1L7hVloA3CGBmXp96ig-T_kHbROWrYxniMlsTxqjXIAvCDEHJ=w506-h303", 
       "width": 506, 
       "type": "image/jpeg", 
       "height": 303
      }, 
      "content": "This is San Francisco\u2019s main Food Stamps office. People call it twelve-thirty-five, as in 1235 Mission Street. The first\u2026", 
      "objectType": "article"
     }
    ], 
    "url": "https://plus.google.com/107033731246200681024/posts/4YiAQHd8Zzv", 
    "content": "This piece about bringing humanity to government services is profound and important. Read it!\ufeff", 
    "plusoners": {
     "totalItems": 92, 
     "selfLink": "https://www.googleapis.com/plus/v1/activities/z12vwdj4gkzdtfakp23gcxizetybvpydh/people/plusoners"
    }, 
    "replies": {
     "totalItems": 13, 
     "selfLink": "https://www.googleapis.com/plus/v1/activities/z12vwdj4gkzdtfakp23gcxizetybvpydh/comments"
    }, 
    "objectType": "note"
   }, 
   "updated": "2014-01-06T18:58:41.680Z", 
   "actor": {
    "url": "https://plus.google.com/107033731246200681024", 
    "image": {
     "url": "https://lh4.googleusercontent.com/-J8nmMwIhpiA/AAAAAAAAAAI/AAAAAAACoLI/0MpUu2BMqTM/photo.jpg?sz=50"
    }, 
    "displayName": "Tim O'Reilly", 
    "id": "107033731246200681024"
   }, 
   "access": {
    "items": [
     {
      "type": "public"
     }
    ], 
    "kind": "plus#acl", 
    "description": "Public"
   }, 
   "verb": "post", 
   "etag": "\"L2Xbn8bDuSErT6QA3PEQiwYKQxM/KLN2StfaXfOhVXlCTLLKLTm82cE\"", 
   "published": "2014-01-06T18:58:41.680Z", 
   "id": "z12vwdj4gkzdtfakp23gcxizetybvpydh"
  }, 
  {
   "kind": "plus#activity", 
   "provider": {
    "title": "Google+"
   }, 
   "title": "Google Teams Up with GM, Honda and Audi to Bring Android to Cars\n\nI'm intrigued but a bit skeptical ...", 
   "url": "https://plus.google.com/107033731246200681024/posts/cNpJxzQhbp9", 
   "object": {
    "resharers": {
     "totalItems": 20, 
     "selfLink": "https://www.googleapis.com/plus/v1/activities/z123glxxykulc3kan04chl3pvxfbszirt5o/people/resharers"
    }, 
    "attachments": [
     {
      "url": "http://www.businessweek.com/articles/2014-01-06/google-teams-with-gm-honda-and-audi-to-bring-android-to-cars", 
      "image": {
       "url": "https://lh3.googleusercontent.com/proxy/DGwkVQ9klCcykeFcUhm97gEXnYzaxO8tvtoLd1TFrVwPGA7JqMa2DjsNRjVv-QzHfJeRW8NGjnjVsX7oj7bJVaaQNPTTOy3RKT1q=w506-h303", 
       "width": 506, 
       "type": "image/jpeg", 
       "height": 303
      }, 
      "fullImage": {
       "url": "http://images.bwbx.io/cms/2014-01-06/0106_cars1_630x354.jpg", 
       "type": "image/jpeg"
      }, 
      "displayName": "http://www.businessweek.com/authors/283-ashlee-vance", 
      "objectType": "article"
     }
    ], 
    "url": "https://plus.google.com/107033731246200681024/posts/cNpJxzQhbp9", 
    "content": "<b>Google Teams Up with GM, Honda and Audi to Bring Android to Cars</b><br /><br />I'm intrigued but a bit skeptical about the newly announced initiative for an "Open Automotive Alliance" to bring Android to automobiles. \u00a0While I love the idea, there are two reasons I am reserving judgment:<br /><br />1. The insatiable desire of vendors to "differentiate" on a standard platform, without thinking about what kinds of differentiation leads to real customer value.<br /><br />2. The failure to understand that in today's world, it's not just about the software, but about data and services.<br /><br />This point is brought home to me every day by my Tesla Model S. While I love the car, I'm incredibly disappointed by its implementation of Google Maps (for which I paid an additional $3000 or so for the "high tech package," without which navigation is disabled). The map display is awesome, especially the second turn-by-turn screen behind the steering wheel, and the directions are Google's. \u00a0But where does the traffic data come from? \u00a0Surely not from Google. \u00a0Any time I suspect there is traffic, I have to pull out my phone and use Google maps before I start, because Tesla's time estimates to destination are pathetically wrong. \u00a0Whatever data services they are using (or not using) to provide traffic estimates are completely useless.<br /><br />In short, I hope that automakers don't add bells and whistles to differentiate themselves, while neglecting core services that customers really depend on.\ufeff", 
    "plusoners": {
     "totalItems": 113, 
     "selfLink": "https://www.googleapis.com/plus/v1/activities/z123glxxykulc3kan04chl3pvxfbszirt5o/people/plusoners"
    }, 
    "replies": {
     "totalItems": 10, 
     "selfLink": "https://www.googleapis.com/plus/v1/activities/z123glxxykulc3kan04chl3pvxfbszirt5o/comments"
    }, 
    "objectType": "note"
   }, 
   "updated": "2014-01-06T18:18:49.000Z", 
   "actor": {
    "url": "https://plus.google.com/107033731246200681024", 
    "image": {
     "url": "https://lh4.googleusercontent.com/-J8nmMwIhpiA/AAAAAAAAAAI/AAAAAAACoLI/0MpUu2BMqTM/photo.jpg?sz=50"
    }, 
    "displayName": "Tim O'Reilly", 
    "id": "107033731246200681024"
   }, 
   "access": {
    "items": [
     {
      "type": "public"
     }
    ], 
    "kind": "plus#acl", 
    "description": "Public"
   }, 
   "verb": "post", 
   "etag": "\"L2Xbn8bDuSErT6QA3PEQiwYKQxM/ZpWDzByoLURnLtgzziRlkjXO7Ek\"", 
   "published": "2014-01-06T18:18:49.000Z", 
   "id": "z123glxxykulc3kan04chl3pvxfbszirt5o"
  }, 
  {
   "kind": "plus#activity", 
   "provider": {
    "title": "Reshared Post"
   }, 
   "title": "Can data and connectivity improve criminal justice and mitigate conflicts?\n\nFind out on Tuesday, January...", 
   "url": "https://plus.google.com/107033731246200681024/posts/XHkDWE5Vq4n", 
   "object": {
    "resharers": {
     "totalItems": 45, 
     "selfLink": "https://www.googleapis.com/plus/v1/activities/z12hyziiizyaih2pp04chl3pvxfbszirt5o/people/resharers"
    }, 
    "attachments": [
     {
      "displayName": "Can data and connectivity improve criminal justice and mitigate conflicts?\n\nFind out on Tuesday, January 7th at 9 a.m. PT/12 p.m. ET. Check out our speakers and register for free here: http://oreil.ly/1h8gb67", 
      "fullImage": {
       "url": "https://lh6.googleusercontent.com/-eXKs1950kh0/UqzEigF351I/AAAAAAAAFzM/TCyrhZ8c6f8/w420-h480/41lxL.jpg", 
       "width": 420, 
       "type": "image/jpeg", 
       "height": 480
      }, 
      "url": "https://plus.google.com/photos/10844250336
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值