我有一個運行代碼使用tweepy的流偵聽器來流推文。它工作得很好,我已經成功運行了幾次,都使用了阿拉伯文,英文和法文關鍵字。Streaming AP:跟蹤關鍵字導致「錯誤:非UTF-8代碼...但沒有聲明的編碼」
出於某種原因,當我插入我的整個組關鍵字(397)錯誤代碼導致讀取
SyntaxError: Non-UTF-8 code starting with '\xd9' in file twitter_streaming_copy.py on line 67, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
很奇怪的是,我試圖運行使用該組的不同部分的代碼關鍵字,它工作正常,只有當我把它們放在一起,停止工作。任何想法?這裏是我的代碼:(我使用python 3)
# Chap02-03/twitter_streaming.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import string
import time
import tweepy
from tweepy import Stream
from tweepy.streaming import StreamListener
consumer_key = ".."
consumer_secret = ".."
access_key = ".-."
access_secret = ".."
class CustomListener(StreamListener):
"""Custom StreamListener for streaming Twitter data."""
def __init__(self, fname):
safe_fname = format_filename(fname)
self.outfile = "stream_%s.jsonl" % safe_fname
def on_data(self, data):
try:
with open(self.outfile, 'a') as f:
f.write(data)
return True
except BaseException as e:
sys.stderr.write("Error on_data: {}\n".format(e))
time.sleep(5)
return True
def on_error(self, status):
if status == 420:
sys.stderr.write("Rate limit exceeded\n")
return False
else:
sys.stderr.write("Error {}\n".format(status))
return True
def format_filename(fname):
"""Convert fname into a safe string for a file name.
Return: string
"""
return ''.join(convert_valid(one_char) for one_char in fname)
def convert_valid(one_char):
"""Convert a character into '_' if "invalid".
Return: string
"""
valid_chars = "-_.%s%s" % (string.ascii_letters, string.digits)
if one_char in valid_chars:
return one_char
else:
return '_'
if __name__ == '__main__':
query = sys.argv[1:] # list of CLI arguments
query_fname = ' '.join(query) # string
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
twitter_stream = Stream(auth, CustomListener(query_fname))
twitter_stream.filter(track=['saudi لبنان', 'iran لبنان', 'iran lebanon', 'ايران لبنان', 'hezbollah lebanon', 'حزب الله لبنان', 'saoudite liban', 'iran liban', 'hezbollah liban'], async=True)
請勿在發佈時手動縮進您的線條。複製代碼並粘貼到問題中,選擇代碼,然後使用工具欄按鈕縮進它。您現在在整個代碼中都會出現奇怪的無效縮進。 –
謝謝你的建議 –