使用Python获取Instagram JSON数据时遇UnicodeEncodeError问题求助
Looks like you've run into a common encoding snag with Windows consoles when handling Unicode characters from Instagram's API. Let's break down what's going on and fix it right up.
The Root of the Problem
Your code actually succeeds in fetching and parsing the Instagram JSON data (the error doesn’t fire in the getResponse function). The issue hits when you try to print the parsed jsonData object. Here’s why:
- Windows Command Prompt/PowerShell uses a default encoding (cp1251 in your case) that doesn’t support all Unicode characters present in Instagram’s response.
- When you print the data, Python tries to encode those Unicode characters using this limited codec—and fails for characters it can’t map to cp1251.
Your Code & Error Context
First, here’s your code for reference:
import urllib.request import json def getResponse(url): operUrl = urllib.request.urlopen(url) if(operUrl.getcode()==200): data = operUrl.read() jsonData = json.loads(data) else: print("Error receiving data", operUrl.getcode()) return jsonData def main(): urlData = "https://www.instagram.com/instagram/?__a=1" jsonData = getResponse(urlData) # print the state id and state name corresponding print(jsonData) if __name__ == '__main__': main()
And the full error traceback:
Traceback (most recent call last):
File "C:\Users\andrey_user\Desktop\ig\test.py", line 21, in [module]
main()
File "C:\Users\andrey_user\Desktop\ig\test.py", line 18, in main
print(jsonData)
File "D:\programs\programing\Python\lib\encodings\cp1251.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 196-197: character maps to [undefined]
Fixes You Can Try
There are a few simple ways to resolve this:
1. Force stdout to use UTF-8 encoding
Add these lines at the very top of your script to override the console’s default encoding:
import sys sys.stdout.reconfigure(encoding='utf-8')
This tells Python to use UTF-8 for all print output, which supports every Unicode character out there.
2. Manually encode output before printing
Instead of printing the raw jsonData object, convert it to a UTF-8-friendly string first:
print(json.dumps(jsonData, ensure_ascii=False).encode('utf-8').decode('utf-8'))
json.dumps(..., ensure_ascii=False)keeps Unicode characters intact instead of escaping them.- Encoding and decoding to UTF-8 ensures the console can handle even special symbols or emojis from Instagram’s data.
3. Write data to a file instead of printing
If you don’t need to view the data in the console, saving it to a UTF-8 encoded file is a reliable alternative:
with open('instagram_data.json', 'w', encoding='utf-8') as f: json.dump(jsonData, f, ensure_ascii=False, indent=4)
This saves formatted JSON to a file that you can open with any UTF-8 compatible text editor (like VS Code, Notepad++, etc.).
Why Your Test Link Worked
The vocab.nic.in test link likely returns JSON without any characters that cp1251 can’t handle, so printing worked smoothly. Instagram’s response includes emojis or rare Unicode symbols that cp1251 doesn’t support, which triggered the error.
内容的提问来源于stack exchange,提问作者Andrey

