a python script to query MIT START website from local machine

匿名 (未验证) 提交于 2019-12-03 07:50:05

问题:

I'm learning Python and the project I've currently set myself includes sending a question from my laptop connected to the net, connect to the MIT START NLP database, enter the question, retrieve the response and display the response. I've read through the "HOWTO Fetch Internet Resources Using urllib2" at docs.python.org but I seem to be missing some poignant bit of this idea. Here's my code:

import urllib import urllib2  question = raw_input("What is your question? ")  url = 'http://start.csail.mit.edu/' values = question  data = urllib.urlencode(values) req = urllib2.Request(url, data) response = urllib2.urlopen(req) the_page = response.read()  print the_page 

and here's the error I'm getting:

Traceback (most recent call last): File "mitstart.py", line 9, in data = urllib.urlencode(values) File "/usr/lib/python2.7/urllib.py", line 1298, in urlencode raise TypeError TypeError: not a valid non-string sequence or mapping object

So I'm thinking that the way I set question in vales was wrong, so I did

values = {question}

and values = (question)

and values = ('question')

with no joy.

(I know, and my response is "I'm learning, it's late, and suddenly my wife decided she needed to talk to me about something trivial while I was trying to figure this out)

Can I get some guidance or at least get pointed in the right direction?

回答1:

urllib.urlencode() doesn't accept a string as an argument.

As @ernie said you should specify query parameter. Also the url is missing the /startfarm.cgi part:

<form method="post" action="startfarm.cgi"> 

Updated example:

import cgi from urllib import urlencode from urllib2 import urlopen  data = urlencode(dict(query=raw_input("What is your question?"))).encode('ascii') response = urlopen("http://start.csail.mit.edu/startfarm.cgi", data)  # extract encoding from Content-Type and print the response _, params = cgi.parse_header(response.headers.get('Content-Type', '')) print response.read().decode(params['charset']) 


回答2:

Note that your error says: TypeError: not a valid non-string sequence or mapping object

So, while you've created values as a string, you need a non-string sequence or a mapping object.

urlencoding requires key value pairs (e.g. a mapping object or a dict), so you generally pass it a dictionary.

Looking at the source for the form, you'll see:

<input type="text" name="query" size="60"> 

This means you should create a dict, something like:

values = { 'query': 'What is your question?' } 

Then you should be able to pass that as the argument to urlencode().



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!