使用Twitter中的一些特殊关键字在python中使用tweepy提取推文

时间:2018-03-04 18:17:20

标签: python python-2.7 twitter tweepy

这是我的代码..我想从Twitter提取一些关键字的推文....我的代码不给任何错误但我没有得到输出文件生成...请帮助我...... ..

import re
import csv
import tweepy
from tweepy import OAuthHandler
#TextBlob perform simple natural language processing tasks.
from textblob import TextBlob



def search():
    #text = e.get() **************************

consumer_key = ''
consumer_secret = ''
access_token = ' '
access_token_secret = ' '
# create OAuthHandler object
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
# set access token and secret
auth.set_access_token(access_token, access_token_secret)
# create tweepy API object to fetch tweets
api = tweepy.API(auth)

def get_tweets(query, count = 300):

   # empty list to store parsed tweets
   tweets = []
   target = open("tweets.txt", 'w',encoding="utf-8")
   t1 = open("review.txt", 'w',encoding="utf-8")
   # call twitter api to fetch tweets
   q=str(query)
   a=str(q+" sarcasm")
   b=str(q+" sarcastic")
   c=str(q+" irony")
   fetched_tweets = api.search(a, count = count)+ api.search(b, count = count)+ api.search(c, count = count)
   # parsing tweets one by one
   print(len(fetched_tweets))

   for tweet in fetched_tweets:

       # empty dictionary to store required params of a tweet
       parsed_tweet = {}
       # saving text of tweet
       parsed_tweet['text'] = tweet.text
       if "http" not in tweet.text:
           line = re.sub("[^A-Za-z]", " ", tweet.text)
           target.write(line+"\n")
           t1.write(line+"\n")
   return tweets

   # creating object of TwitterClient Class
   # calling function to get tweets
tweets = get_tweets(query =text, count = 20000)

root.mainloop()

从这段代码我也没有得到输出生成的文件。谁能告诉我我做错了什么? 提前谢谢!

1 个答案:

答案 0 :(得分:2)

我只做了一些细微的改动,它对我来说非常合适。删除或评论了一些不必要的陈述(如审核文件)。将open函数更改为io.open,因为我有python版本2.7。这是运行代码,希望它有所帮助!!

`

import re
import io
import csv
import tweepy
from tweepy import OAuthHandler
#TextBlob perform simple natural language processing tasks.
#from textblob import TextBlob


consumer_key = 'sz6x0nvL0ls9wacR64MZu23z4'
consumer_secret = 'ofeGnzduikcHX6iaQMqBCIJ666m6nXAQACIAXMJaFhmC6rjRmT'
access_token = '854004678127910913-PUPfQYxIjpBWjXOgE25kys8kmDJdY0G'
access_token_secret = 'BC2TxbhKXkdkZ91DXofF7GX8p2JNfbpHqhshW1bwQkgxN'
# create OAuthHandler object
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
# set access token and secret
auth.set_access_token(access_token, access_token_secret)
# create tweepy API object to fetch tweets
api = tweepy.API(auth)




def get_tweets(query, count = 300):

    # empty list to store parsed tweets
    tweets = []
    target = io.open("mytweets.txt", 'w', encoding='utf-8')
    # call twitter api to fetch tweets
    q=str(query)
    a=str(q+" sarcasm")
    b=str(q+" sarcastic")
    c=str(q+" irony")
    fetched_tweets = api.search(a, count = count)+ api.search(b, count = count)+ api.search(c, count = count)
    # parsing tweets one by one
    print(len(fetched_tweets))

    for tweet in fetched_tweets:

        # empty dictionary to store required params of a tweet
        parsed_tweet = {}
        # saving text of tweet
        parsed_tweet['text'] = tweet.text
        if "http" not in tweet.text:
            line = re.sub("[^A-Za-z]", " ", tweet.text)
            target.write(line+"\n")
    return tweets

    # creating object of TwitterClient Class
    # calling function to get tweets
tweets = get_tweets(query ="", count = 20000)

`