获取“raise JSONDecodeError(“期望值”,s,错误值)从无到有json.decoder.jsondecode

2024-09-28 05:41:09 发布

您现在位置:Python中文网/ 问答频道 /正文

当我试图运行下面的代码时,我得到了jsondecodererror错误。我不知道怎么修理它。在

我正在尝试流媒体推特。它对几条tweet很好用,突然间我得到了错误,流停止了请帮助我如何修复这个问题? 更新完整代码

from tweepy import API
from tweepy import Cursor
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
from textblob import TextBlob
from googletrans import Translator
import json
import re
import csv

ACCESS_TOKEN = "2446723783-cqGTmWPdtJI5HhCT7UmmET2xVY7xlA1RqlBQdQl"
ACCESS_TOKEN_SECRET = "6ZRIqd9HDCgZxsR6iMUbt6eOgk1YVsz59lEsvL30eFnPf"
CONSUMER_KEY = "slWhR4Z6VOjp0R3ojx1B2a4pr"
CONSUMER_SECRET = "6OrySqqUIwQM8ioBycqbStWizM6KkLCMFecjXPvbVmsrsw6eNT"

Count = 0


translator = Translator()
class StdOutListener(StreamListener):

    def on_data(self, data):
        global Count
        #Data has the JSON STring
        y = json.loads(data)

        print("--------------------")
        try:
            y = y['extended_tweet']['full_text']
            y = str(y)
            ## Portuguese to English conversion block
            translations = translator.translate(y, dest='en')
            print(translations.origin, '\n -> \n', translations.text)
            z = translations.text
            a = translations.origin
            row = [data,a,z]
            with open(fetched_tweets_filename, 'a') as csvFile:
                writer = csv.writer(csvFile)
                writer.writerow(row)

            Count = Count + 1
            print(Count)
        except KeyError:
            y = y['text']
            y = str(y)
            ## Portuguese to English conversion block
            translations = translator.translate(y, dest='en')
            print(translations.origin, '\n -> \n', translations.text)
            z = translations.text
            a = translations.origin
            row = [data,a,z]
            with open(fetched_tweets_filename, 'a') as csvFile:
                writer = csv.writer(csvFile)
                writer.writerow(row)

            Count = Count + 1
            print(Count)

        ### Sentiment Analysis block    
        analysis = TextBlob(z)

        if analysis.sentiment.polarity > 0:
            print("sentiment is positiv")
        elif analysis.sentiment.polarity == 0:
            print("sentiment is Neutral")
        else:
            print("sentiment is Negative")
        print("--------------------\n")    

        return True

    def on_error(self,status):
        print(status)


if __name__ == "__main__":
    listener=StdOutListener()
    auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

    fetched_tweets_filename = "datas3.csv"

    stream = Stream(auth, listener)
    stream.filter(track=['como'],languages=['pt'])



追踪

^{pr2}$

我想它发生在某些推特上,当它发生的时候我怎么能通过它呢。 我不确定我是否完全错了。在

当我试图写一个包含一个特殊字符的字符串,比如“teamo”时,也会出现下面的错误❤️ " 以下是错误

--------------------
RT @PortalMessi: “Messi seria para mim, como um jogador que eu vi jogar, o número 1. Meu pai sempre disse que o melhor foi Pelé, mas eu não… 
 -> 
 RT @PortalMessi: "Messi would be for me, as a player I saw play, the number 1. My father always said that the best was Pele, but I did not ...
1
sentiment is positiv
--------------------

--------------------
RT @felipeneto: Informo que a dívida de 170 mil reais do Botafogo comigo como pessoa física foi integralmente perdoada, junto com os juros… 
 -> 
 RT @felipeneto: I report that Botafogo's debt of 170,000 reais with me as a natural person has been totally forgiven, along with interest ...
2
sentiment is positiv
--------------------

--------------------
@MaluMonteiro08 óh @deus como pode tamanha beleza????? 
 -> 
 @ MaluMonteiro08 ohh @deus how can such beauty ?????
3
sentiment is Neutral
--------------------

--------------------
Meu pai amava. 
Descanse em paz, rainha! ❤️ 
 -> 
 My father loved it.
Rest in peace, queen! (I.e.
Traceback (most recent call last):
  File "C:\Users\Stramzik\Desktop\SentizAnalyzer\Version3.py", line 32, in on_data
    y = y['extended_tweet']['full_text']
KeyError: 'extended_tweet'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Stramzik\Desktop\SentizAnalyzer\Version3.py", line 84, in <module>
    stream.filter(track=['como'],languages=['pt'])
  File "C:\Users\Stramzik\AppData\Local\Programs\Python\Python37\lib\site-packages\tweepy\streaming.py", line 453, in filter
    self._start(is_async)
  File "C:\Users\Stramzik\AppData\Local\Programs\Python\Python37\lib\site-packages\tweepy\streaming.py", line 368, in _start
    self._run()
  File "C:\Users\Stramzik\AppData\Local\Programs\Python\Python37\lib\site-packages\tweepy\streaming.py", line 300, in _run
    six.reraise(*exc_info)
  File "C:\Users\Stramzik\AppData\Local\Programs\Python\Python37\lib\site-packages\six.py", line 693, in reraise
    raise value
  File "C:\Users\Stramzik\AppData\Local\Programs\Python\Python37\lib\site-packages\tweepy\streaming.py", line 269, in _run
    self._read_loop(resp)
  File "C:\Users\Stramzik\AppData\Local\Programs\Python\Python37\lib\site-packages\tweepy\streaming.py", line 331, in _read_loop
    self._data(next_status_obj)
  File "C:\Users\Stramzik\AppData\Local\Programs\Python\Python37\lib\site-packages\tweepy\streaming.py", line 303, in _data
    if self.listener.on_data(data) is False:
  File "C:\Users\Stramzik\Desktop\SentizAnalyzer\Version3.py", line 55, in on_data
    writer.writerow(row)
  File "C:\Users\Stramzik\AppData\Local\Programs\Python\Python37\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 5967-5968: character maps to <undefined>
[Finished in 5.5s with exit code 1]
[shell_cmd: python -u "C:\Users\Stramzik\Desktop\SentizAnalyzer\Version3.py"]
[dir: C:\Users\Stramzik\Desktop\SentizAnalyzer]
[path: C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\Webex\Webex\Applications;C:\Program Files\IBM\SPSS\Modeler\18.1.1\ext\bin\spss.TMWBServer\bin;C:\Program Files (x86)\Sennheiser\SoftphoneSDK\;C:\Users\Stramzik\AppData\Local\Programs\Python\Python37;C:\Users\Stramzik\AppData\Local\Programs\Python\Python37\Scripts;C:\Users\Stramzik\AppData\Roaming\nltk_data;C:\Users\Stramzik\AppData\Local\Microsoft\WindowsApps;C:\Users\Stramzik\AppData\Local\Box\Box Edit\]

Tags: inpyimportdatalocallineusersappdata
1条回答
网友
1楼 · 发布于 2024-09-28 05:41:09
  1. 检查您正在获取的数据是否实际是json,如果出现某些错误,您可能无法获取有效的json数据,也可以尝试将json.loads挡在里面。在
  2. 使用正确的编码将数据编码为字节,同时写入文件。在
import json
import csv


class StdOutListener(StreamListener):

    def on_data(self, data):
        global Count
        #Data has the JSON STring


        print("          ")
        try:
            y = json.loads(data)
            y = y['extended_tweet']['full_text']
            y = str(y)
            ## Portuguese to English conversion block
            translations = translator.translate(y, dest='en')
            print(translations.origin, '\n -> \n', translations.text)
            z = translations.text.encode("utf-8")
            a = translations.origin.encode("utf-8")
            data = data.encode("utf-8")
            row = [data,a,z]
            with open(fetched_tweets_filename, 'a') as csvFile:
                writer = csv.writer(csvFile)
                writer.writerow(row)

            Count = Count + 1
            print(Count)
        except KeyError:
            y = y.get('text', '')   # change this to get
            y = str(y)
            ## Portuguese to English conversion block
            translations = translator.translate(y, dest='en')
            print(translations.origin, '\n -> \n', translations.text)
            z = translations.text.encode("utf-8")
            a = translations.origin.encode("utf-8")
            row = [data, a, z]
            with open(fetched_tweets_filename, 'a') as csvFile:
                writer = csv.writer(csvFile)
                writer.writerow(row)

            Count = Count + 1
            print(Count)
        except Exception as e:
            print(str(e))

        ### Sentiment Analysis block
        analysis = TextBlob(z)

        if analysis.sentiment.polarity > 0:
            print("sentiment is positiv")
        elif analysis.sentiment.polarity == 0:
            print("sentiment is Neutral")
        else:
            print("sentiment is Negative")
        print("          \n")

        return True

    def on_error(self,status):
        print(status)


if __name__ == "__main__":
    listener=StdOutListener()
    auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

    fetched_tweets_filename = "datas3.csv"

    stream = Stream(auth, listener)
    stream.filter(track=['como'],languages=['pt'])

相关问题 更多 >

    热门问题