如何只接收时间戳、推文文本和帐户名称,而不是我在使用 twitter api 时收到的输出?
Posted
技术标签:
【中文标题】如何只接收时间戳、推文文本和帐户名称,而不是我在使用 twitter api 时收到的输出?【英文标题】:How to just recieve timestamp, tweet-text and account name instead of this output I receive while working with twitter api? 【发布时间】:2013-12-27 04:36:42 【问题描述】:这是我的代码。而不是我收到的所有垃圾值,我怎样才能输出推文、用户名和时间戳的文本?
import oauth2 as oauth
import urllib2 as urllib
access_token_key = "secret"
access_token_secret = "secret
consumer_key = "secret"
consumer_secret = "secret"
_debug = 0
oauth_token = oauth.Token(key=access_token_key, secret=access_token_secret)
oauth_consumer = oauth.Consumer(key=consumer_key, secret=consumer_secret)
signature_method_hmac_sha1 = oauth.SignatureMethod_HMAC_SHA1()
http_method = "GET"
http_handler = urllib.HTTPHandler(debuglevel=_debug)
https_handler = urllib.HTTPSHandler(debuglevel=_debug)
def twitterreq(url, method, parameters):
req = oauth.Request.from_consumer_and_token(oauth_consumer,
token=oauth_token,
http_method=http_method,
http_url=url,
parameters=parameters)
req.sign_request(signature_method_hmac_sha1, oauth_consumer, oauth_token)
headers = req.to_header()
if http_method == "POST":
encoded_post_data = req.to_postdata()
else:
encoded_post_data = None
url = req.to_url()
opener = urllib.OpenerDirector()
opener.add_handler(http_handler)
opener.add_handler(https_handler)
response = opener.open(url, encoded_post_data)
return response
def fetchsamples():
url = "https://userstream.twitter.com/1.1/user.json"
parameters = []
response = twitterreq(url, "GET", parameters)
for line in response:
print line.strip()
if __name__ == '__main__':
fetchsamples()
我收到的输出是这样的
"created_at":"Mon Dec 09 15:10:41 +0000 2013","id":410063960465891329,"id_str":"410063960465891329","text":"Django Hacker,下一代电子商务技术http://t.co/de6gdIqJvL #Hyderabad","source":"\u003ca href=\"http://jobs.hasgeek.com\" rel=\"nofollow\"\u003eHasGeek Job Board\u003c/a \u003e","截断":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":"id":260654014 ,"id_str":"260654014","name":"HasGeek 工作委员会","screen_name":"hasjob","location":"","url":"http://jobs.hasgeek.in", "description":"HasGeek Job Board 的职位列表。","protected":false,"followers_count":1530,"friends_count":1,"listed_count":32,"created_at":"Fri Mar 04 09:21 :06 +0000 2011","favourites_count":0,"utc_offset":19800,"time_zone":"Mumbai","geo_enabled":true,"verified":false,"statuses_count":10528,"lang":" zh","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0 DEED","profile_background_image_url":"http://abs.twimg.com/images/themes/theme1/bg.png","profile_background_image_url_https":"https://abs.twimg.com/images/themes/theme1/ bg.png","profile_background_tile":false,"profile_image_url":"http://pbs.twimg.com/profile_images/1271127705/logo-star_normal.png","profile_image_url_https":"https://pbs.twimg. com/profile_images/1271127705/logo-star_normal.png","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true ,"default_profile":true,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null,"geo":null,"coordinates":null,"place":null, "contributors":null,"retweet_count":0,"favorite_count":0,"entities":"hashtags":["text":"Hyderabad","indices":[69,79]],"符号":[],"urls":["url":"http://t.co/de6gdIqJvL","expanded_url":"http://hsgk.in/1aOhn8V","display_url":"hsgk .in/1aOhn8V","索引":[46,68]],"u ser_mentions":[],"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"medium","lang":"en"
【问题讨论】:
【参考方案1】:它是 json。在python中,在一个非常基础的层面上,你可以导入json库,将tweet作为python对象从json加载并打印如下字段。
import json
...
tweetObj = json.loads(response)
print tweetObj['user']['id_str'], tweetObj['created_at'], tweetObj['text']
【讨论】:
以上是关于如何只接收时间戳、推文文本和帐户名称,而不是我在使用 twitter api 时收到的输出?的主要内容,如果未能解决你的问题,请参考以下文章