【发布时间】:2018-07-16 11:17:10
【问题描述】:
我正在尝试在 python 中使用 tweepy 仅保存乌尔都语推文。我正在使用 3.6 版本的 python。如果我打开文件,乌尔都语的数据不会保存在文件中,我只能看到用户名,而不是乌尔都语的推文。这是我的代码
同样的代码适用于英语。
import re
import io
import csv
import tweepy
from tweepy import OAuthHandler
#from textblob import TextBlob
consumer_key = "xxxxxxxxxxxxxxxxxxxxx"
consumer_secret = "xxxxxxxxxxxxxxxxxx"
access_key = "xxxxxxxxxxxxxxxxxxxxx"
access_secret = "xxxxxxxxxxxxxxxxxxxx"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
# set access token and secret
auth.set_access_token(access_key, access_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("newsfileurdu.csv", 'w', encoding='utf-8')
# call twitter api to fetch tweets
q=str(query)
a=str(q+" اردو")
b=str(q+" خبریں")
c=str(q+" خبریں اردو")
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)
【问题讨论】: