gpt4 book ai didi

python - 未绑定(bind)本地错误: local variable

转载 作者:行者123 更新时间:2023-12-01 05:51:38 26 4
gpt4 key购买 nike

我的代码如下:

search_request = urllib2.Request(url,data=tmp_file_name,headers={'X-Requested-With':'WoMenShi888XMLHttpRequestWin'})
#print search_request.get_method()
try:
search_response = urllib2.urlopen(search_request)
except urllib2.HTTPError:
pass
html_data = search_response.read()
print html_data

但是当我运行它时我得到这个:

Traceback (most recent call last):
File "G:\MyProjects\python\lfi_tmp.py", line 78, in <module>
print hello_lfi()
File "G:\MyProjects\python\lfi_tmp.py", line 70, in hello_lfi
html_data = search_response.read()
UnboundLocalError: local variable 'search_response' referenced before assignment

我尝试添加

global search_response

再次运行,我得到这样的异常

Traceback (most recent call last):
File "G:\MyProjects\python\lfi_tmp.py", line 78, in <modul
print hello_lfi()
File "G:\MyProjects\python\lfi_tmp.py", line 70, in hello_
html_data = search_response.read()
NameError: global name 'search_response' is not defined

最佳答案

如果您收到 HTTPError,则表示您没有 search_response 变量。所以这一行:

html_data = search_response.read()

引发错误,因为您正在尝试访问未声明的 search_response。我认为您应该像这样替换 html_data = search_response.read() 行:

search_request = urllib2.Request(url,data=tmp_file_name,headers={'X-Requested-With':'WoMenShi888XMLHttpRequestWin'})
#print search_request.get_method()
try:
search_response = urllib2.urlopen(search_request)
html_data = search_response.read() #New here
except urllib2.HTTPError:
html_data = "error" #And here

print html_data

关于python - 未绑定(bind)本地错误: local variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14021596/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com