urllib.urlencode: TypeError not a valid non-string sequence or mapping object

时光总嘲笑我的痴心妄想 提交于 2019-12-01 08:37:13

The urlencode function does not take a single string as input, it takes a something like a dictionary.

data = urlencode({'key': apiKey, 'q': source_word, ...)
urllib2.urlopen("http://....", data)

From the Documentation,

urllib.urlencode(query[, doseq])

Convert a mapping object or a sequence of two-element tuples to a “percent-encoded” string, suitable to pass to urlopen() above as the optional data argument. This is useful to pass a dictionary of form fields to a POST request. The resulting string is a series of key=value pairs separated by '&' characters, where both key and value are quoted using quote_plus() above. When a sequence of two-element tuples is used as the query argument, the first element of each tuple is a key and the second is a value.

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