开发者

How come I'm getting this Python error in my code?

开发者 https://www.devze.com 2023-01-31 19:36 出处:网络
import urllib, urllib2 def URLRequest(url, params, method=\"GET\"): if method == \"POST\": return urllib2.Request(url, data=urllib.encode(params))<<<< 31
import urllib, urllib2
def URLRequest(url, params, method="GET"):
    if method == "POST":
        return urllib2.Request(url, data=urllib.encode(params))  <<<< 31
    else:
        r开发者_开发百科eturn urllib2.Request(url + "?" + urllib.encode(params))

'module' object has no attribute 'encode', line 31


The error message is correct: the urllib module does not have an encode() function. The function name is urlencode(), so you would call:

urllib.urlencode(params)

Python docs for the function: http://docs.python.org/library/urllib.html#urllib.urlencode

0

精彩评论

暂无评论...
验证码 换一张
取 消