#Request methodpost from urllib import request , parse import json def fanyi(content): data={ 'kw':content } data=parse.urlencode(data) # print(len(data)) base_url = 'http://fanyi.baidu.com/sug' # Post headers = { "Content-Length": len(data), # 动态计算data长度 "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36" } #字符串转bytes req=request.Request(url=base_url,data=bytes(data,encoding='utf-8'),headers=headers) response=request.urlopen(req) html=response.read() html=html.decode('utf-8') json_data=json.loads(html) #comb into json format print (json_data) #Organize data for item in json_data[ 'data' ]: print (item[ 'k' ] , item[ 'v' ]) if __name__=='__main__': content = input ( 'Please enter the content you want to translate:' ) fanyi(content)
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/apple/PycharmProjects/stage4/spider/20180305/05post.py
Please enter what you want to translate: just
{'errno': 0, 'data': [{'k': 'just', 'v': 'adv. just; just; just, just; just about; adj. just, reasonable; proper; legal; correct'}, {'k': 'justice', 'v': 'n. justice; justice; legal sanctions; judge, judge;'}, {'k': 'justify', 'v': 'vt. justify... ; justify; explain; vi. organize; justify;'}, {'k': 'just do it', 'v': ' just do it;'}, {'k': 'just now', 'v': ' just now; just now; now; immediately;'}]}
just adv. correct
justice n. justice; justice; legal sanctions; judge, judge;
justify vt. justify; justify; justify; explain; vi. organize the page; justify;
just do it
just now; just now; now; immediately;
Process finished with exit code 0