首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >python使用urllib2和urllib get请求问题

python使用urllib2和urllib get请求问题
EN

Stack Overflow用户
提问于 2019-11-22 12:59:22
回答 1查看 75关注 0票数 0

当我使用curl get请求成功,但使用python调用get请求失败时,任何人都可以帮助我!

代码语言:javascript
运行
复制
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'

获取请求成功。

代码语言:javascript
运行
复制
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: unhashable type

任何人都知道如何使用get request by urllib2和urllib模型。非常感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-26 17:42:10

我发现我们可以定义这个方法来调用api get data

代码语言:javascript
运行
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58987787

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档