About Saving Tweets from Python

Asked 2 years ago, Updated 2 years ago, 99 views

I want to save the tweet below, but it doesn't work.Running python intento.py > result.dat
I wrote

#-*-coding:utf-8-*-
from tweet.streaming import StreamListener
from tweet import OAuthHandler
from tweet import Stream
import json

# Variables that contain the user credentials to access Twitter API
access_token='access_token_example'
access_token_secret = 'access_token_secret'
consumer_key='commer_srcret_example'
consumer_secret='consumer_secret_example'


# This is a basic listener that just prints received tweets to stdout.
class StdOutListener (StreamListener):
    defon_data(self,data):
        json_load=json.loads(data)
        texts=json_load ['text']
        coded=texts.encode('utf-8')
        s=str(coded)
        print(s[2:-1])
        return True

    default_error(self, status):
        print(status)

auth=OAuthHandler(consumer_key,consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream=Stream(auth, StdOutListener())

# This line filter Twitter Streams to capture data by the keywords: 'python', 'javascript', 'ruby'
OKINAWA = [127.672482, 26.08887, 128.275025, 26.851451 ]
stream.filter(track=[u'灣',], languages=['ja'], locations=OKINAWA)    

Run
[root@localhost Desktop] #python intento.py
0o8m3o1e
Thank you
2tNC9CYlGoicJz So I'm going to start with a small amount of money. I'm going to take care of the rest of the accounts 👐 You just have to pile them up. い… �ANS_AMRAI@hanaFFRK Please don't commit any more
じThe crown driver of the car in front of you is bad. python intento.py > result.dat

If you open a file (result.dat) on your desktop, nothing appears.
If anyone knows how to do it, I would appreciate it if you could tell me.
Thank you for your cooperation.

python twitter

2022-09-30 19:32

2 Answers

Try changing on_data to python intento.py>result.txt


    defon_data(self,data):
        print(data[2:-1])
        return True


2022-09-30 19:32

Wouldn't it be output if I stopped the program with CTRL+C?
In this case, the print() output is buffered.

Below is the smallest sample that can reproduce the symptom.

test_print.py

import time

while True:
    print("a")
    time.sleep(1)

Running on python test_print.py>>result.dat and looking at the output on tailf result.dat from another console does not produce anything while running, but stops at CTRL+C.

print("a", flush=True) means that the print will be printed immediately when it is called.


2022-09-30 19:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.