我有个奇怪的问题。我在做一个Twitter Tweepy MySQL收集项目。你知道吗
我对我的剧本非常满意。例如,如果我对“伦敦”进行收集-没问题。你知道吗
然而,当我尝试收集非拉丁语标签(例如“Šזלטווב”)时,我甚至不能保存脚本,更不用说运行它了。你知道吗
我按Ctrl+S-不保存。我尝试运行脚本-我得到“源代码必须保存确定保存?”,点击Ok-什么都没发生。你知道吗
阿拉伯文和希伯来文标签都会出现这种情况,所以我只能认为这里存在unicode问题—但是当我收集到一个拉丁语标签时,碰巧产生了非拉丁字母表的结果,它们被打印并保存到MySQL中,没有任何问题。你知道吗
非常感谢您的指点。
罗宾
脚本如下:
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import MySQLdb
import time
import json
conn = MySQLdb.connect ('nyub', 'nyub', 'nyub', 'nyub', charset='utf8')
c = conn.cursor()
ckey = "'nyub'"
csecret = "'nyub'"
atoken = "-'nyub'"
asecret = "'nyub'"
class listener (StreamListener):
def on_data(self, data):
try:
tweet = json.loads(data)
created_at = tweet ["created_at"]
identity = tweet ["id"]
text = tweet ["text"]
status_reply = tweet ["in_reply_to_status_id"]
user_reply = tweet ["in_reply_to_user_id"]
screen_name_reply = tweet ["in_reply_to_screen_name"]
user_id = tweet ["user"]["id"]
user_screen_name = tweet["user"]["screen_name"]
user_location = tweet["user"]["location"]
user_url = tweet["user"]["url"]
user_description = tweet["user"]["description"]
user_followers = tweet["user"]["followers_count"]
user_friends = tweet["user"]["friends_count"]
user_statuses = tweet["user"]["statuses_count"]
user_created = tweet["user"]["created_at"]
user_lang = tweet["user"]["lang"]
coordinates = tweet ["coordinates"]
place = tweet ["place"]
rt_count = tweet ["retweet_count"]
fav_count = tweet ["favorite_count"]
hashtags = []
for hashtag in tweet ["entities"]["hashtags"]:
hashtags.append(hashtag["text"])
urls = []
for url in tweet ["entities"]["urls"]:
urls.append(url["expanded_url"])
c.execute ("INSERT INTO luck (timestamp, tweet_created_at, tweet_id, tweet_text, in_reply_status, in_reply_user_id, in_reply_screen_name, user_id, user_screen_name, user_location, user_url, user_description, user_followers_count, user_friends_count, users_statuses_count, user_created_at, user_lang, tweet_coords, tweet_place, tweet_retweet_count, tweet_fav_count, hashtags, urls) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",
((time.time()), created_at, identity, text, status_reply, user_reply, screen_name_reply, user_id, user_screen_name, user_location, user_url, user_description, user_followers, user_friends, user_statuses, user_created, user_lang, coordinates, place, rt_count, fav_count, str(hashtags), str(urls)))
conn.commit ()
print (text)
return True
except BaseException, e:
print "failed on data", str(e)
time.sleep(5)
def on_error (self, status):
print status
auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
twitterStream = Stream(auth, listener())
twitterStream.filter(track=["#מזל_טוב"])
将其添加为python文件的第一行(如果与#/bin/bash一起使用,则添加第二行)
用这个
而不是
后者只适用于python3
在脚本开头添加以下内容(第一行):
当然要确保它确实保存在utf-8中。你知道吗
相关问题 更多 >
编程相关推荐