Python uses requests and urllib2 two ways to invoke the Turing man interface

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.