[英]json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) python
[英]Python script: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
我分叉了這個repo ,以便將我的話語網站變成 static 之一。 但是,我不斷收到此消息:
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
它適用於大多數帖子,因此可能是因為我要存檔的論壇太大。
此行很可能會導致問題posts_json = response.json()['post_stream']['posts']
。 任何幫助將不勝感激!
# 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()
可能是您沒有從 get 請求中取回有效的 json。 如果它是 2xx 代碼,您是否檢查過您在此處獲得的狀態代碼? 您還可以添加
response.raise_for_status()
看看這是否失敗以及你得到哪個異常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.