擅长:python、mysql、java
<p>还有一些例子:</p>
<pre><code> import httplib2
import xml.etree.ElementTree as ET
def request(query):
query = urllib.urlencode({'input':query})
app_id = "Q6254U-URKKHH9JLL"
wolfram_api = "http://api.wolframalpha.com/v2/query?appid="+app_id+"&format=plaintext&podtitle=Result&"+query
resp, content = httplib2.Http().request(wolfram_api)
return content
def response(query):
content = request(query)
root = ET.fromstring(content)
error = root.get('error')
success = root.get('success')
numpods = root.get('numpods')
answer= ''
if success and int(numpods) > 0 :
for plaintext in root.iter('plaintext'):
if isinstance(plaintext.text, str) :
answer = answer + plaintext.text
return answer
elif error:
return "sorry I don't know that"
request("How old is the queen")
</code></pre>