我想将一些从twitter获取的数据插入django数据库。我收到了一个错误,我在互联网上尝试了所有解决方案。什么都没有改变
的myproject / MyApp的/ get_tweet.py
from tweets.models import Tweet, TwitterUser
import tweepy
import time
def get_tweets(users, tweet_count):
"""
users = list of tweeter users
tweet_count = number of tweets planned to be fetched per user
"""
consumer_key = #
consumer_secret = #
access_token = #
access_token_secret = #
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
for user in users:
try:
image_url = api.get_user(user).profile_image_url_https
db_user = TwitterUser(user_name=user, profile_image=image_url)
db_user.save()
user_id = api.get_user(user).id
tweet_id = None
count = tweet_count
tweet_list = ['']
#To fetch 10 tweets (except replies and retweets) if exist
while count>0 and len(tweet_list)>0:
tweet_list = api.user_timeline(id=user_id, count=tweet_count, max_id=tweet_id)
for tweet in tweet_list:
tweet_id = tweet.id - 1
#Blocks replies and retweets of user
if tweet.in_reply_to_status_id==None and tweet.text[:4]!='RT @':
db_tweet = Tweet(user=db_user, date=tweet.created_at, tweet_text=tweet.text)
db_tweet.save()
count -= 1
except tweepy.RateLimitError:
time.sleep(60*15)
except tweepy.TweepError:
break
get_tweets('twitter', 10)
我得到的是:
Traceback (most recent call last):
File "/home/abdullahsecer/PycharmProjects/Test/tweets/get_tweet.py", line 1, in <module>
from tweets.models import Tweet, TwitterUser
File "/home/abdullahsecer/PycharmProjects/Test/tweets/models.py", line 4, in <module>
class TwitterUser(models.Model):
File "/home/abdullahsecer/PycharmProjects/Test/tweets/models.py", line 5, in TwitterUser
user_name = models.CharField(max_length=20)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 1081, in __init__
super(CharField, self).__init__(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 161, in __init__
self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 48, in __getattr__
self._setup(name)
File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 42, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
当我尝试通过添加以下行来向数据库添加数据时发生错误:
db_user = TwitterUser(user_name=user, profile_image=image_url)
db_user.save()
db_tweet = Tweet(user=db_user, date=tweet.created_at, tweet_text=tweet.text)
db_tweet.save()