Recently from the Internet to see an interesting Turing, can be based on different information intelligent reply, such as you send a "tell a joke", it will give you a reply to a joke, or "Beijing weather" can reply to the weather situation, or English words and then give you a reply to Chinese interpretation. The official documentation has PHP and Java calls, I'll get a python.
Register to get API KEY
This step is simple, just register an account to see your API KEY. This key will need to be used when we send a GET request later.
Pythoh Invocation Example
It is also relatively easy to use, mainly analog post requests. Then parse the JSON data. You can use requests, or you can use Urllib and urllib2. You may not have installed requests, you can click this link to view official documents and install, request greatly simplifies the process of sending HTTP requests.
ImportJsonImportRequestsImportUrllibImportUrllib2key =' *********************** ' # Change to your API KEYURL =' Http://www.tuling123.com/openapi/api 'Req_info =u ' tell a joke '. Encode (' Utf-8 ') query = {' key ': KEY,' Info ': Req_info}headers = {' Content-type ':' text/html ',' CharSet ':' Utf-8 '}# Method One, get content with requests module get moder = Requests.get (URL, params=query, headers=headers) res = R.textPrintJson.loads (res). Get (' text '). Replace (' <br> ',' \ n ')# Method Two, get content with Urllib and URLLIB2 librarydata = Urllib.urlencode (query) req = Urllib2. Request (URL, data) F = urllib2.urlopen (req). Read ()PrintJson.loads (f). Get (' text '). Replace (' <br> ',' \ n ')
The content of the output is the parsed text content, such as this is sent to "tell a joke", and then run the script will output a joke. You can put it on your public platform as a life assistant.
from a personal blog:Python uses requests and urllib2 two ways to invoke the Turing man interface
Python uses requests and urllib2 two ways to invoke the Turing man interface