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

2024-04-24 09:58:10 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的代码…我想从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()

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


Tags: totextimporttokenapisearchgetsecret
1条回答
网友
1楼 · 发布于 2024-04-24 09:58:10

我只是做了一些小小的改变,对我来说效果很好。删除或注释了一些不必要的语句(如审阅文件)。已将打开函数更改为io.打开因为我有Python2.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)

`

相关问题 更多 >