Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 如何只接收时间戳、tweet文本和帐户名,而不是在使用twitterapi时接收的输出?_Python 2.7_Twitter_Twitter Oauth - Fatal编程技术网

Python 2.7 如何只接收时间戳、tweet文本和帐户名,而不是在使用twitterapi时接收的输出?

Python 2.7 如何只接收时间戳、tweet文本和帐户名,而不是在使用twitterapi时接收的输出?,python-2.7,twitter,twitter-oauth,Python 2.7,Twitter,Twitter Oauth,这是我的密码。除了我接收到的所有垃圾值,我怎么能只输出tweet的文本、用户名和时间戳呢 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=

这是我的密码。除了我接收到的所有垃圾值,我怎么能只输出tweet的文本、用户名和时间戳呢

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 #海得拉巴,“来源”:“\u003ca href=\”http://jobs.hasgeek.com\“rel=\“nofollow\”\u003eHasGeek作业板\u003c/a\u003e,“截断”:false,“在对状态id的回复中”:null,“in-reply_to_-status_-id_-str”:null,“in-reply_-to_-user_-id”:null,“in-reply_-to_-user_-id_-str”:null,“user”:{“id”:260654014,“id_-str”:“260654014”,“姓名”:“HasGeek工作板”,“屏幕名”:“hasjob”,“位置”:“url”:”http://jobs.hasgeek.in,“说明”:“来自HasGeek工作板的工作列表”。,“受保护”:false追随者数量:1530,“朋友数量”:1,“列出数量”:32,“创建时间”:“Fri Mar 04 09:21:06+0000 2011”,“收藏夹数量”:0,“utc偏移量”:19800,“时区”:“孟买”,“地理位置启用”:真,“验证”:假,“状态数量”:10528,“朗”:“恩”,“贡献者启用”:假,“是翻译者”:假,“个人资料背景颜色”:“C0契据”配置文件\背景\图像\ url:“http://abs.twimg.com/images/themes/theme1/bg.png,“配置文件\u背景\u图像\u url\u https”:https://abs.twimg.com/images/themes/theme1/bg.png,“配置文件\背景文件\平铺”:false,“配置文件\图像\ url”:http://pbs.twimg.com/profile_images/1271127705/logo-star_normal.png,“配置文件\u图像\u url\u https”:https://pbs.twimg.com/profile_images/1271127705/logo-star_normal.png“,”配置文件链接颜色“:”0084B4“,”配置文件侧栏边框颜色“:”C0dect“,”配置文件侧栏填充颜色“:”DDEEF6“,”配置文件文本颜色“:”333333“,”配置文件使用背景图片“:”true“,”默认配置文件“:”true“,”默认配置文件图片“:”false“,”follow“,”null“,”follow“,”请求“:”null,”通知:null},“地理”:null,“坐标”:null,“地点”:null,“贡献者”:null,“转发次数”:0,“收藏次数”:0,“实体”:{“hashtags”:[{“text”:“hydrabad”,“index”:[69,79]}],“符号”:[],“url”:[{“url”:”http://t.co/de6gdIqJvL,“扩展的url”:http://hsgk.in/1aOhn8V“,“显示url”:“hsgk.in/1aOhn8V”,“索引”:[46,68]}],“用户提及”:[]}”,“favorited”:false,“retweeted”:false,“可能敏感”:false,“filter\u level”:“Middle”,“lang”:“en”}

它是json。在python中,在一个非常基本的级别上,您可以导入json库,从json中将tweet作为python对象加载,并打印如下字段

import json
...

tweetObj = json.loads(response)
print tweetObj['user']['id_str'], tweetObj['created_at'], tweetObj['text']

在python中,在一个非常基本的级别上,您可以导入json库,从json将tweet作为python对象加载,并打印如下字段

import json
...

tweetObj = json.loads(response)
print tweetObj['user']['id_str'], tweetObj['created_at'], tweetObj['text']