Scrapy json response convert in utf-8 encode Scrapy json response convert in utf-8 encode json json

Scrapy json response convert in utf-8 encode


First of all, {"url": "/book/category/271/\u09a8\u09be\u099f\u0995", "name": "\u09a8\u09be\u099f\u0995"} is valid JSON data

>>> import json>>> d = json.loads('''{"url": "/book/category/271/\u09a8\u09be\u099f\u0995", "name": "\u09a8\u09be\u099f\u0995"}''')>>> print(d['name'])নাটক

and any program interpreting this data should understand (i.e. decode) the characters just fine. Python json module calls this ensure_ascii:

If ensure_ascii is true (the default), all non-ASCII characters in the output are escaped with \uXXXX sequences, and the result is a str instance consisting of ASCII characters only.

This is what Scrapy feed exporter uses by default for JSON output.

But if you need the output JSON file to use another encoding, such as UTF-8, you can use Scrapy's FEED_EXPORT_ENCODING setting.

FEED_EXPORT_ENCODING = 'utf-8'


At settings.py,add the following line:FEED_EXPORT_ENCODING = 'utf-8'


To run in command-line use the option "--set FEED_EXPORT_ENCODING=utf-8":

scrapy runspider --set FEED_EXPORT_ENCODING=utf-8 .\TheScrapyScript.py -o TheOutputFile.json