【问题标题】:Twitter stream API gives JSONDecodeError("Expecting value", s, err.value) from NoneTwitter 流 API 从 None 给出 JSONDecodeError("Expecting value", s, err.value)
【发布时间】:2017-05-11 02:00:02
【问题描述】:

我正在使用 Twitter 的流 API(通过 tweepy)来收集符合特定条件的推文,但是当我使用 json.loads() 解析创建的 jsonl 文件时,我收到以下错误:

File "twitter_time_series.py", line 19, in <module> 
    tweet = json.loads(line)

File "C:\Program Files\Anaconda3\lib\json\__init__.py", line 319, in loads 
    return _default_decoder.decode(s)

File "C:\Program Files\Anaconda3\lib\json\decoder.py", line 339, in decode
   obj, end = self.raw_decode(s, idx=_w(s, 0).end())

File "C:\Program Files\Anaconda3\lib\json\decoder.py", line 357, in raw_decode 
    raise JSONDecodeError("Expecting value", s, err.value) from None

json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 1)

我无法确定为什么与其他类型的 jsonl 文件一样(例如获取 twitter 时间线等),这不会发生。 任何人都可以帮助我吗?谢谢!

我正在使用基本的流推文:

auth = get_twitter_auth() 

twitter_stream = Stream(auth, CustomListener(query_fname)) 

twitter_stream.filter(track=['curiosity'], async=True)

用于加载 json:

fname = sys.argv[1] 

with open(fname, "r") as f: 

    for line in f:
      tweet = json.loads(line)

我正在使用 python 3.5 和 tweepy 3.3.0 版本,这是 json 文件的一行:

{"created_at":"Mon Dec 26 16:03:06 +0000 2016",

"id":813414846033170432,

"id_str":"813414846033170432",

"text":"Battle proper and at greater risk https:\/\/t.co\/W6U9Irgst9","source":"\u003ca href=\"http:\/\/twitter.com\" rel=\"nofollow\"\u003eTwitter Web Client\u003c\/a\u003e",

"truncated":false,

"in_reply_to_status_id":null,

"in_reply_to_status_id_str":null,

"in_reply_to_user_id":null,

"in_reply_to_user_id_str":null,

"in_reply_to_screen_name":null,

"user":{"id":544570972,"id_str":"544570972","name":"Fay Moore","screen_name":"MooreFay","location":"Maryland","url":"http:\/\/faymoore.wordpress.com","description":"Author, writer for hire, crazy woman in a crazy world","protected":false,"verified":false,"followers_count":482,"friends_count":322,"listed_count":22,"favourites_count":92,
"statuses_count":17326,"created_at":"Tue Apr 03 20:30:46 +0000 2012","utc_offset":-18000,"time_zone":"Eastern Time (US & Canada)","geo_enabled":false,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_link_color":"1DA1F2","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/3252060596\/e06263f9a226ca0e3f83915812c331e6_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/3252060596\/e06263f9a226ca0e3f83915812c331e6_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/544570972\/1360842914","default_profile":true,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},

"geo":null,

"coordinates":null,

"place":null,

"contributors":null,

"is_quote_status":false,

"retweet_count":0,

"favorite_count":0,

"entities":{"hashtags":[],"urls":[{"url":"https:\/\/t.co\/W6U9Irgst9","expanded_url":"http:\/\/a.msn.com\/01\/en-us\/BBxzSWF?ocid=st","display_url":"a.msn.com\/01\/en-us\/BBxzS\u2026","indices":[81,104]}],"user_mentions":[],"symbols"[]},

"favorited":false,

"retweeted":false,

"possibly_sensitive":false,

"filter_level":"low",

"lang":"en",

"timestamp_ms":"1482768186468"}

【问题讨论】:

标签: python json twitter stream tweepy


【解决方案1】:

写入文件时,在Listener类中,可以尝试:

def on_data(self, data):
    with open('filename.json', 'a', newline='\n') as f:
        f.write(data)

我在 Windows 机器上也遇到过这个问题。这是因为 Windows 使用 CR LF 行尾,而 Linux 使用 LF。

如果您尝试阅读一条推文,我认为它不会出现错误,正如您在此处看到的那样:

json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 1)  

这更可能是 json python 包而不是 TwitterAPI 中的错误。

见:The modern way: use newline=''

更多关于行尾的信息:On Wikipedia,一如既往。

【讨论】:

    【解决方案2】:

    在for循环里面,你可以试试:

      tweet = json.loads(line.decode())
    

    或删除 for 循环并尝试:

      tweets = json.load(f)
    

    【讨论】:

      【解决方案3】:

      我刚才也遇到了同样的问题。

      我尝试使用json分析器测试json字符串,发现格式有些错误。

      比如,False 应该是“False”,None 应该是“None”。

      我的意思是首先你应该检查json字符串的格式是否正确,一种方法是在专门转换json格式的网站上进行测试。

      希望答案对你有点帮助。

      【讨论】:

        猜你喜欢
        • 2022-06-11
        • 2020-04-19
        • 2020-09-05
        • 2021-09-29
        • 2019-08-06
        • 1970-01-01
        • 1970-01-01
        • 2019-09-18
        • 1970-01-01
        相关资源
        最近更新 更多