Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.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将Tweets流式传输到.txt文件_Python_Json_Python 3.x_Text_Rodeo - Fatal编程技术网

使用Python将Tweets流式传输到.txt文件

使用Python将Tweets流式传输到.txt文件,python,json,python-3.x,text,rodeo,Python,Json,Python 3.x,Text,Rodeo,我有下面的代码,想把推特流写入一个文本文件。有没有办法将输出包含在同一代码中的文本文件中,并将其保存在工作目录中?我是一个IDE爱好者,真的不喜欢使用控制台。我是python新手(2周),我是R/R Studio用户 我知道我可以使用: filename.py > output.txt 我目前正在使用Rodeo,Python 3.6.1 import oauth2 as oauth import urllib.request as urllib api_key = "##" api_s

我有下面的代码,想把推特流写入一个文本文件。有没有办法将输出包含在同一代码中的文本文件中,并将其保存在工作目录中?我是一个IDE爱好者,真的不喜欢使用控制台。我是python新手(2周),我是R/R Studio用户

我知道我可以使用:

filename.py > output.txt
我目前正在使用Rodeo,Python 3.6.1

import oauth2 as oauth
import urllib.request as urllib

api_key = "##"
api_secret = "##"
access_token_key = "##-##"
access_token_secret = "##"

_debug = 0

oauth_token    = oauth.Token(key=access_token_key, secret=access_token_secret)
oauth_consumer = oauth.Consumer(key=api_key, secret=api_secret)

signature_method_hmac_sha1 = oauth.SignatureMethod_HMAC_SHA1()

http_method = "GET"


http_handler  = urllib.HTTPHandler(debuglevel=_debug)
https_handler = urllib.HTTPSHandler(debuglevel=_debug)

'''
Construct, sign, and open a twitter request
using the hard-coded credentials above.
'''
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)

    f = open("output.txt", "wb")

def fetchsamples():
  url = "https://stream.twitter.com/1.1/statuses/sample.json"
  parameters = []
  response = twitterreq(url, "GET", parameters)
  for line in response:
    f.write(line)

if __name__ == '__main__':
  fetchsamples()

# f.close()

除了前面的评论外,我还建议检查一下堆栈溢出问题:

引述:

如果您想在Python中执行此操作,那么您可以编写:

with open('out.txt', 'w') as f:
  f.write(something)`
显然,这只是一个微不足道的例子。显然,您可以在
块中执行更多操作


您应该创建一个示例。@stephernauch。我混合了你的实现和一些谷歌搜索。在我的编辑上面。我打开txt“con”,然后逐行处理流媒体。我认为这避免了字符串与字节的冲突。如何指定写入.txt文件的路径?现在它将它写入我的wd。谢谢!提供
response
对象示例,我们需要知道哪些对象存储在那里:
bytes
str
您说过您可以只使用
filename.py>output.txt
,但不想?这是最有效的方法,你试过吗?如果你打算将来做任何主要的编程,你应该习惯命令行/终端/等等。@Jeremy,是的,我先在控制台上做的。我只是喜欢把所有的代码都放在一个地方。