簡體   English   中英

Tweepy:抓取實時流媒體推文並保存到.csv文件中

[英]Tweepy: crawl live streaming tweets and save in to a .csv file

在閱讀使用Tweepy的流媒體並完成此示例之后 我嘗試用tweepy Api編寫一個tweepy應用程序來抓取實時流數據並將其保存到.csv文件中。 當我運行我的代碼時,它返回空的csv文件('OutputStreaming.csv'),列名為['Date','Text','Location','Number_Follower','User_Name','Friends_count','Hash_Tag],不是流推文。 我也試着做,在這種方式也是這一個 ,但我得到我的代碼同出認沽: -

def on_status(self, status):
with open('OutputStreaming.csv', 'w') as f:
f.write(['Author,Date,Text')
writer = csv.writer(f)
writer.writerow([status.created_at.strftime("%Y-%m-%d \
                %H:%M:%S")status.text.encode,
                status.location,
                status.Number_of_follwers,
                status.author.screen_name,
                status.friends_count])

我被困。 我無法弄清楚代碼的問題在哪里,我的代碼如下: -

import tweepy
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import json #data
#Variables that contains the user credentials to access Twitter API 
access_token = "***"
access_token_secret = "***"
consumer_key = "***"
consumer_key_secret = "***"
auth = tweepy.OAuthHandler(consumer_key, consumer_key_secret)
auth.set_access_token(access_token, access_token_secret)
#setup api
api = tweepy.API(auth) 
class CustomStreamListener(tweepy.StreamListener):
    def on_data(self,data):
        if data:
           tweet_json = json.loads(data)
        if tweet_json:
           if not tweet_json['text'].strip().startswith('RT '):
              Created = data.created_at.strftime("%Y-%m-%d-%H:%M:%S")`
              Text = data.text.encode('utf8')
              Location = data.location('utf8')
              Follower = data.Number_of_follwers('utf8')
              Name = data.author.screen_name('utf8')
              Friend = data.friends_count('utf8')
              with open('OutputStreaming.csv', 'a') as f:
                   writer = csv.writer(f)
                   writer.writerow([Created, Text ,Loaction\ 
                   ,Follower ,Name ,Friend,status.entities.get('hashtags')])
                   Time.sleep(10)
           return True
    def on_error(self, status_code):
        if status_code == 420:
           return False
        else:
           print >> sys.stderr, 'Encountered error with status code:',\ 
           status_code
    def on_timeout(self):
        print >> sys.stderr, 'Timeout...'
      return True
# Writing csv titles
with open('OutputStreaming.csv', 'a') as f:
writer = csv.writer(f)
writer.writerow(['Date', 'Text', 'Location','Number_Follower', 
'User_Name', 'Friends_count','Hash_Tag'])
if __name__ == '__main__':
l = CustomStreamListener()
streamingAPI = tweepy.streaming.Stream(api.auth, l)
streamingAPI.filter(track=['#Yoga','#Meditation'])

這是一個有效的代碼:

#!/usr/bin/python3
# coding=utf-8

import tweepy

SEP = ';'
csv = open('OutputStreaming.csv','a')
csv.write('Date' + SEP + 'Text' + SEP + 'Location' + SEP + 'Number_Follower' + SEP + 'User_Name' + SEP + 'Friends_count\n')

class MyStreamListener(tweepy.StreamListener):

    def on_status(self, status):
        Created = status.created_at.strftime("%Y-%m-%d-%H:%M:%S")
        Text = status.text.replace('\n', ' ').replace('\r', '').replace(SEP, ' ')
        Location = ''
        if status.coordinates is not None:
            lon = status.coordinates['coordinates'][0]
            lat = status.coordinates['coordinates'][1]
            Location = lat + ',' + lon       
        Follower = str(status.user.followers_count)
        Name = status.user.screen_name
        Friend = str(status.user.friends_count)
        csv.write(Created + SEP + Text + SEP + Location + SEP + Follower + SEP + Name + SEP + Friend + '\n')

    def on_error(self, status_code):
        print(status_code)

consumer_key = '***'
consumer_secret = '***'
access_token = '***'
access_token_secret = '***'

# stream
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
myStream = tweepy.Stream(auth, MyStreamListener())
myStream.filter(track=['#Yoga','#Meditation'])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM