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

后端 未结 1 1604
旧时难觅i
旧时难觅i 2021-01-13 18:48

I am trying to run following code but it is giving me below error:

Traceback (most recent call last):  File \"put_message.py\", line 43, in tra         


        
相关标签:
1条回答
  • 2021-01-13 19:10

    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.

    0 讨论(0)
提交回复
热议问题