简体   繁体   English

在python中使用tweepy从Twitter提取带有一些特殊关键字的tweet

[英]extract tweets with some special keywords from twitter using tweepy in python

here is my code..i want to extract tweets from twitter with some keywords....my code dont give any errors but i am not getting the output file generated...please help me........ 这是我的代码..我想从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()

From this code i am nor getting the output generated file. 从这个代码我也没有得到输出生成的文件。 Can anyone tell me what i am doing wrong ? 谁能告诉我我在做什么错? Thanks in advance! 提前致谢!

I just made some slight changes and it was working perfectly for me. 我只是做了一些细微的更改,它对我来说效果很好。 Removed or commented some unnecessary statements (like the review file). 删除或评论了一些不必要的语句(例如审阅文件)。 Changed the open function to io.open since I have python version 2.7. 由于我具有python版本2.7,因此将open函数更改为io.open。 Here is the running code, hope it helps!! 这是正在运行的代码,希望对您有所帮助!!

` `

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)

` `

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM