Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么python代码不检索阿拉伯语推文,而是与其他语言一起工作?为什么它返回随机字符?_Python_Twitter_Arabic_Twitter Streaming Api - Fatal编程技术网

为什么python代码不检索阿拉伯语推文,而是与其他语言一起工作?为什么它返回随机字符?

为什么python代码不检索阿拉伯语推文,而是与其他语言一起工作?为什么它返回随机字符?,python,twitter,arabic,twitter-streaming-api,Python,Twitter,Arabic,Twitter Streaming Api,我的代码是用python3编写的,我以前用它来实时播放英语推文。但是,当搜索阿拉伯语查询时,它会返回所有符号和随机字符的tweet。这是一个屏幕截图和代码。(注:我是一名编码初学者)(谢谢!)以下是我的代码: 导入twitter、json、csv 消费者密钥=“” 消费者机密=“” OAUTH_令牌=“” OAUTH_令牌_秘密=“” auth=twitter.oauth.oauth(oauth_令牌,oauth_令牌,oauth密钥, 消费者密钥,消费者密钥) twitter\u api=tw

我的代码是用python3编写的,我以前用它来实时播放英语推文。但是,当搜索阿拉伯语查询时,它会返回所有符号和随机字符的tweet。这是一个屏幕截图和代码。(注:我是一名编码初学者)(谢谢!)以下是我的代码:

导入twitter、json、csv
消费者密钥=“”
消费者机密=“”
OAUTH_令牌=“”
OAUTH_令牌_秘密=“”
auth=twitter.oauth.oauth(oauth_令牌,oauth_令牌,oauth密钥,
消费者密钥,消费者密钥)
twitter\u api=twitter.twitter(auth=auth)
#设置要写入的文件
csvfile=open('tweets\u extended.csv','w')
csvwriter=csv.writer(csvfile,分隔符=“|”)
#这里有一个函数,它可以删除可能中断的字符
#我们将导入Excel并用空格替换它们
#它还执行unicode位
def getVal(val):
clean=“”
如果val:
val=val.replace('|','')
val=val.replace('\n','')
val=val.replace('\r','')
clean=val.encode('utf-8')
还清
q=“سلمان”#逗号分隔的术语列表可以放在这里
打印('筛选曲目=“%s””%(q,)的公共时间线)
twitter\u stream=twitter.TwitterStream(auth=twitter\u api.auth)
stream=twitter\u stream.status.filter(track=q)
对于流中的tweet:
尝试:
如果tweet[‘截断’]:
tweet_text=tweet['extended_tweet']['full_text']
其他:
tweet_text=tweet['text']
#将值写入文件
csvwriter.writerow([
tweet['created_at'],
getVal(tweet['user']['screen_name']),
getVal(tweet_文本),
getVal(tweet['user']['location']),
tweet['user']['statuses\u count'],
推特['user']['followers\u count'],
tweet['user']['lang'],
tweet['user']['id'],
])
#在屏幕上打印一些东西,主要是为了让我们看到正在发生的事情。。。
打印(tweet['user']['screen_name'].encode('utf-8')、tweet['text'].encode('utf-8'))
除异常作为错误外:
打印(错误)
通过

getVal
中使用
val.encode('utf-8')
。。。。你为什么这么做?不要那样做。谢谢,我删除了那条线,它不起作用@我刚刚编辑了你的帖子,删除了你的OAuth秘密。您可能想访问您的Twitter应用程序并重新生成新的应用程序。我认为您的问题来自Excel。请在您使用的
val.encode('utf-8')
中尝试我的答案。。。。你为什么这么做?不要那样做。谢谢,我删除了那条线,它不起作用@我刚刚编辑了你的帖子,删除了你的OAuth秘密。您可能想访问您的Twitter应用程序并重新生成新的应用程序。我认为您的问题来自Excel。在这里试试我的答案
import twitter,json,csv

CONSUMER_KEY = '<consumer key>'
CONSUMER_SECRET = '<consumer secret>'
OAUTH_TOKEN = '<oauth token>'
OAUTH_TOKEN_SECRET = '<oauth token secret>'

auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET,
                       CONSUMER_KEY, CONSUMER_SECRET)

twitter_api = twitter.Twitter(auth=auth)

# setup a file to write to
csvfile = open('tweets_extended.csv', 'w')
csvwriter = csv.writer(csvfile, delimiter='|')

#  heres a function that takes out characters that can break
#  our import into Excel and replaces them with spaces
#  it also does the unicode bit
def getVal(val):
    clean = ""
    if val:
        val = val.replace('|', ' ')
        val = val.replace('\n', ' ')
        val = val.replace('\r', ' ')
        clean = val.encode('utf-8')
    return clean


 q = "سلمان" # Comma-separated list of terms can go here
 print ('Filtering the public timeline for track="%s"' % (q,))

 twitter_stream = twitter.TwitterStream(auth=twitter_api.auth)

 stream = twitter_stream.statuses.filter(track=q)

for tweet in stream:
    try:
        if tweet['truncated']:
            tweet_text = tweet['extended_tweet']['full_text']
        else:
            tweet_text = tweet['text']
        # write the values to file
        csvwriter.writerow([
            tweet['created_at'],
            getVal(tweet['user']['screen_name']),
            getVal(tweet_text),
            getVal(tweet['user']['location']),
            tweet['user']['statuses_count'],
            tweet['user']['followers_count'],
            tweet['user']['lang'],
            tweet['user']['id'],
            ])
        # print something to the screen, mostly so we can see what is going on...
        print (tweet['user']['screen_name'].encode('utf-8'), tweet['text'].encode('utf-8'))
    except Exception as err:
        print (err)
        pass