[PYTHON] How to call a POST request that supports Japanese (Shift-JIS) with requests

Overview

--When making a request with the POST method using the requests module of python, URL encode it, but be careful when passing Japanese parameter values.

--Some sites decode in Shift-JIS format, in which case the requester must also encode and call in Shift-JIS format.

--If you pass it to requests.post in dict format, the default encoding will be performed, so by using the urllib module to encode the URL, make it str type and pass it directly to requests, the default encoding is also avoided and encoded with Shift-JIS. It is possible to pass parameters.

Solution

import requests
import urllib.parse

#value of post parameter (example)
item = 'Hoge'
post_data = {'KEY1':item}

# shift-Encode with jis and make it str type
urlencode_post_data = urllib.parse.urlencode(post_data, encoding='shift-jis')

headers = {'content-type': 'text/html; charset=Shift_JIS'}
response = requests.post(<Target URL>, urlencode_post_data, headers=headers)

At the end

――When dealing with Japanese, such a character code problem comes up. When I searched for a solution, I couldn't find a solution that was applicable to sites other than Japanese-speaking countries, so I solved it by trial and error on my own. I will post it if it helps even a little.

Recommended Posts

How to call a POST request that supports Japanese (Shift-JIS) with requests
How to call a function
How to write a metaclass that supports both python2 and python3
How to send a request to the DMM (FANZA) API with python
How to add a package with PyCharm
How to use Japanese with NLTK plot
How to display python Japanese with lolipop
How to enter Japanese with Python curses
How to draw a bar graph that summarizes multiple series with matplotlib
How to read a CSV file with Python 2/3
How to send a message to LINE with curl
How to draw a 2-axis graph with pyplot
How to develop a cart app with Django
How to make a dictionary with a hierarchical structure.
[Tentative] How to convert a character string to Shift_jis with kivy-ios Memo kivy v1.8.0
How to create a multi-platform app with kivy
[Go] How to write or call a function
[Python] How to handle Japanese characters with openCV
How to make Linux compatible with Japanese keyboard
A new form of app that works with GitHub: How to make GitHub Apps
How to convert / restore a string with [] in python
[Python] How to draw a line graph with Matplotlib
How to create a submenu with the [Blender] plugin
How to get a logged-in user with Django's forms.py
How to convert a class object to a dictionary with SQLAlchemy
A simple Python HTTP server that supports Range Requests
How to make a shooting game with toio (Part 1)
Create a chatbot that supports free input with Word2Vec
Make a function to describe Japanese fonts with OpenCV
[Python] How to write a docstring that conforms to PEP8
How to post a ticket from the Shogun API
[Python] How to create a 2D histogram with Matplotlib
[Python] How to call a c function from python (ctypes)
[Python] How to draw a scatter plot with Matplotlib
How to deploy a web app made with Flask to Heroku
How to make a Cisco Webex Teams BOT with Flask
How to put a hyperlink to "file: // hogehoge" with sphinx-> pdf
How to install NPI + send a message to line with python
Try to play with the uprobe that supports Systemtap directly
How to kill a process instantly with Python's Process Pool Executor
How to convert an array to a dictionary with Python [Application]
How to output a document in pdf format with Sphinx
How to use the __call__ method in a Python class
Generate a password that is easy to remember with apg
How to create a flow mesh around a cylinder with snappyHexMesh
How to print characters as a table with Python's print function
How to make a simple Flappy Bird game with pygame
How to write a test for processing that uses BigQuery
Post a message to Google Hangouts Chat with a thread (Python)
How to display a list of installable versions with pyenv
How to extract other than a specific index with Numpy
A story about how to deal with the CORS problem
How to build a python2.7 series development environment with Vagrant
A story that didn't work when I tried to log in with the Python requests module
How to turn ugly code into plain code that isn't a beautiful object-oriented design with Python refactoring
Post to slack with Python 3
How to update with SQLAlchemy?
How to cast with Theano
How to hack a terminal
How to Alter with SQLAlchemy?
How to separate strings with','