sloria / TextBlob

Simple, Pythonic, text processing--Sentiment analysis, part-of-speech tagging, noun phrase extraction, translation, and more.
https://textblob.readthedocs.io/
MIT License
9.11k stars 1.13k forks source link

HTTP Error 400: Bad Request #402

Closed Isabelane closed 7 months ago

Isabelane commented 2 years ago
for tweet in ts.search_tweets_iterable(tso): #tso metadata
    print('created_at: ',tweet['created_at'], 'user_id: ',tweet['id_str'], 'Tweet: ',tweet['text'],' Like:',tweet['favorite_count'], 'RTs:', tweet['retweet_count'],
    'geo:', tweet['geo'],'Source:',tweet['source'] )

    created_at = tweet ['created_at']
    user_id = tweet['id_str']
    texto = tweet['text']
    Like = tweet['favorite_count']
    RTs = tweet['retweet_count']
    geo = tweet['geo']
    Source = tweet['source']

    with open("tweet.json", "a+") as output:

        data = { "created_at": created_at,
                  "user_id": user_id,
                  "tweet":texto,
                  "favorite_count":Like,
                 "retweet_count":RTs,
                    "geo":geo,
                  "Source":Source}

        output.write("{}\n".format(json.dumps(data)))

except TwitterSearchException as e: print(e)

pd = pd.read_json("tweet.json", lines = True)

contador=0

def clean_tweet(tweet): return ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)", " ", tweet).split()) def analize_sentimentEN(tweet): print("Executando função: analize_sentimentEN") global contador contador+=1 print(contador) analysis = TextBlob(tweet) time.sleep(1.6)

if analysis.detect_language() != 'en':
    analysis = TextBlob(str(analysis.translate(from_lang= 'pt', to_lang='en')))
    time.sleep(1.8)    

if analysis.sentiment.polarity > 0:
    return 1 
elif analysis.sentiment.polarity == 0:
    return 0
else:
    return -1

listaTweets = pd['tweet'].tolist()

listaResultadoSentimentos = []

for elemento in listaTweets: listaResultadoSentimentos.append(analize_sentimentEN(elemento))

pd['Sentimento'] = listaResultadoSentimentos

listaResultado = pd["Sentimento"].tolist() listaClassificada = []

for elemento in listaResultado: print(elemento) if elemento == 1: listaClassificada.append("Positivo") elif elemento == 0: listaClassificada.append("Neutro") elif elemento == -1: listaClassificada.append("Negativo")

pd["Classificacao"] = listaClassificada


HTTPError Traceback (most recent call last)

in () 75 76 for elemento in listaTweets: ---> 77 listaResultadoSentimentos.append(analize_sentimentEN(elemento)) 78 79 pd['Sentimento'] = listaResultadoSentimentos 9 frames /usr/lib/python3.7/urllib/request.py in http_error_default(self, req, fp, code, msg, hdrs) 647 class HTTPDefaultErrorHandler(BaseHandler): 648 def http_error_default(self, req, fp, code, msg, hdrs): --> 649 raise HTTPError(req.full_url, code, msg, hdrs, fp) 650 651 class HTTPRedirectHandler(BaseHandler): HTTPError: HTTP Error 400: Bad Request
Isabelane commented 2 years ago

@sloria help please

FrancescoMalluzzo commented 2 years ago

Same problem

sloria commented 7 months ago

language detection and translation have been removed from textblob. it is recommended that you use the google translate API directly