简体   繁体   English

Python 脚本:json.decoder.JSONDecodeError:预期值:第 1 行第 1 列(字符 0)

[英]Python script: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I forked this repo in order to make my discourse site into a static one.我分叉了这个repo ,以便将我的话语网站变成 static 之一。 But, I keep getting this message:但是,我不断收到此消息:

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

It works for most posts, so it could be because the forum I am trying to archive is too large.它适用于大多数帖子,因此可能是因为我要存档的论坛太大。

This line is most likely causing trouble posts_json = response.json()['post_stream']['posts'] .此行很可能会导致问题posts_json = response.json()['post_stream']['posts'] Any help would be greatly appreciated!任何帮助将不胜感激!

# Function that writes out each individual topic page
def write_topic(topic_json):
    topic_download_url = base_url + '/t/' + topic_json['slug'] + '/' + str(topic_json['id'])
    topic_relative_url = 't/' + topic_json['slug'] + '/' + str(topic_json['id'])
    try:
        os.makedirs(topic_relative_url)
    except Exception as err:
        print ('in write_topic error:', 'make directory')
    response = requests.get(topic_download_url + '.json', cookies=jar)
    # posts_json will contain only the first 20 posts in a topic
    posts_json = response.json()['post_stream']['posts']
    # posts_stream will grab all of the post ids for that topic
    posts_stream = response.json()['post_stream']['stream']
    # get rid of first 20 in stream, as they are already in posts_json
    posts_stream = posts_stream[20:]
    # break stream into a list of list chunks of n posts each for lighter requests
    n = 9999999
    chunked_posts_stream = [posts_stream[i * n:(i + 1) * n] for i in range((len(posts_stream) + n - 1) // n)]
    posts_download_url = base_url + '/t/' + str(topic_json['id']) + '/posts.json?'
    # make a request for the content associated with each post id
    # chunk and append it to the posts_json list
    for chunk in chunked_posts_stream:
        formatted_posts_list = ""
        for post_id in chunk:
            formatted_posts_list = formatted_posts_list + 'post_ids[]=' + str(post_id) + '&'
        response = requests.get(posts_download_url + formatted_posts_list, cookies=jar)
        posts_2_json = response.json()['post_stream']['posts']
        posts_json.extend(posts_2_json)
    # generate that HTML
    post_list_string = ""
    for post_json in posts_json:
        post_list_string = post_list_string + post_row(post_json)
    topic_file_string = topic_template \
        .replace("<!-- TOPIC_TITLE -->", topic_json['fancy_title']) \
        .replace("<!-- JUST_SITE_TITLE -->", str(site_title.text)) \
        .replace("<!-- ARCHIVE_BLURB -->", archive_blurb) \
        .replace("<!-- POST_LIST -->", post_list_string)

    f = open(topic_relative_url + '/index.html', 'w')
    f.write(topic_file_string)
    f.close()

It might be that you don't get back a valid json from the get request.可能是您没有从 get 请求中取回有效的 json。 Have you checked the status code that you got here if it is a 2xx code?如果它是 2xx 代码,您是否检查过您在此处获得的状态代码? You can also add您还可以添加

response.raise_for_status()

to see if that fails and which exception you get.看看这是否失败以及你得到哪个异常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 json.decoder.JSONDecodeError:期望值:第 1 行第 1 列(字符 0)python - json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) python Python:json.decoder.JSONDecodeError:期望值:第 1 行第 1 列(字符 0) - Python: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) json.decoder.JSONDecodeError:期望值:第2行第1列(char 1)错误 - json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 1) error 错误:json.decoder.JSONDecodeError:期望值:第 1 行第 1 列(字符 0) - ERROR: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) 奇怪的“json.decoder.JSONDecodeError:期望值:第 1 行第 1 列(字符 0)” - WEIRD "json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)" json.decoder.JSONDecodeError:期望值:第 1 行第 1 列(字符 0) - json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) ##json.decoder.JSONDecodeError:期望值:第 1 行第 1 列(字符 0)## - ##json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)## json.decoder.JSONDecodeError:期望值:第 1 行第 1 列(字符 0)Scrapy - json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) Scrapy 什么是json.decoder.JSONDecodeError:期望值:第1行第1列(字符0) - What is json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) 随机 json.decoder.JSONDecodeError:期望值:第 1 行第 1 列(字符 0) - Random json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM