Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python使用urllib2和urllib get请求问题_Python_Api_Get_Urllib_Urllib2 - Fatal编程技术网

python使用urllib2和urllib get请求问题

python使用urllib2和urllib get请求问题,python,api,get,urllib,urllib2,Python,Api,Get,Urllib,Urllib2,当我使用curl-get-request成功,但使用python调用get-request失败时,任何人都可以提供帮助 shell script: curl -X GET -H 'Content-type: application/json' -H 'Authorization: Bearer '$token'' -d '[ { "id":"535985", "language":"EN", "Detailes":{"LibraryId":"KF_2gl9xZYKX7TJi66" }, "Key

当我使用curl-get-request成功,但使用python调用get-request失败时,任何人都可以提供帮助

shell script:
curl -X GET -H 'Content-type: application/json' -H 'Authorization: Bearer '$token'' -d '[ { "id":"535985", "language":"EN", "Detailes":{"LibraryId":"KF_2gl9xZYKX7TJi66" }, "KeyID":"SF_cY1tKhYiocNluBB" } ]' 'https://XXXX.com'
获取请求成功

python Script:
data ={ "id":"535985", "language":"EN", "Detailes":{"LibraryId":"KF_2gl9xZYKX7TJi66" }, "KeyID":"SF_cY1tKhYiocNluBB" }
headers = {'content-type': 'application/json', 'Accept': 'application/json', 'Authorization': 'Bearer %s' % (token)}
proxy = urllib2.ProxyHandler({})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
DataForGet=urllib.urlencode(data)
NewUrl= apilink + "?" + DataForGet
request = urllib2.Request(NewUrl, headers)
response = urllib2.urlopen(request, timeout=300)
message = response.read()
返回消息 TypeError:不可损坏的类型


任何人都知道如何通过urllib2和urllib模型使用get请求。非常感谢

我发现我们可以定义这个方法来调用api获取数据

class RequestMethod(urllib2.Request,object):
    def __init__(self, url, method=None,data=None, headers={},origin_req_host=None, unverifiable=False):
        self.method = method
        super(RequestMethod,self).__init__(url, data=data,headers=headers,origin_req_host=origin_req_host,unverifiable=unverifiable)
    def get_method(self):
        if self.method != None:
            return self.method
        else:
            return super(RequestMethod,self).get_method()
    def set_method(self,method):
        self.method = method
    def setMethodRequest(url,method):
        return RequestMethod(url,method=method)

为什么不使用?因为服务器python版本是2.7,我们没有访问pip安装请求模型的权限。请将您的问题包含在完整的回溯中。