urllib 中的post请求
import urllib.request
import urllib.parse
url = 'http://fanyi.baidu.com/v2transapi'
word = 'meizi'
formdata = {
'from' : 'en',
'to' : 'zh',
'query':word,
'transtype':'realtime',
'simple_means_flag' :'3',
'sign': '800673.578704',
'token' :'b4af6d5be2544feca4407f376e0b5635'
}
headers = {
'Host': 'fanyi.baidu.com',
# 'Connection': 'keep-alive',
# 'Content-Length': '135',
# 'Accept': '*/*',
'Origin': 'http://fanyi.baidu.com',
'X-Requested-With': 'XMLHttpRequest',
'User-Agent':' Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36',
# 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Referer': 'http://fanyi.baidu.com/translate?aldtype=16047',
# 'Accept-Encoding': 'gzip, deflate',
'Accept-Language':' zh-CN,zh;q=0.9',
'Cookie': 'REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; PSTM=1526888660; BIDUPSID=3AC4D74B022A729BF6F805111B8EDE9A; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BAIDUID=6B0F99164495B82513E4B8AD44956B6A:FG=1; H_PS_PSSID=26357_1465_21093_20928; PSINO=1; locale=zh; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1527585078,1527585086,1527585119,1527592236; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1527592236; to_lang_often=%5B%7B%22value%22%3A%22zh%22%2C%22text%22%3A%22%u4E2D%u6587%22%7D%2C%7B%22value%22%3A%22en%22%2C%22text%22%3A%22%u82F1%u8BED%22%7D%5D; from_lang_often=%5B%7B%22value%22%3A%22pt%22%2C%22text%22%3A%22%u8461%u8404%u7259%u8BED%22%7D%2C%7B%22value%22%3A%22en%22%2C%22text%22%3A%22%u82F1%u8BED%22%7D%2C%7B%22value%22%3A%22zh%22%2C%22text%22%3A%22%u4E2D%u6587%22%7D%5D',
}
request = urllib.request.Request(url=url,headers=headers)
formdata = urllib.parse.urlencode(formdata).encode()
response = urllib.request.urlopen(request,formdata)
print(response.read().decode())