我正在从Internet存档中提取图像,作为对某些python代码的测试,并且正在合并请求模块。我的代码如下:(注意,不是整个代码,只是相关的部分)

    image_results = []
    image_hashes = []
    session = requests.Session()


    for image in image_list:
        if txtUrl not in image:
            continue
    try:
        self.rslBox.AppendText("[v] Downloading %s" % image + "\n")
        self.rslBox.Refresh()
        response = session.get(image)
    except:
        self.rslBox.AppendText("[!] Failed to download: %s" % image + "\n")
        self.rslBox.Refresh()
#        continue

    if "image" in response.headers['content-type']:

        sha1 = hashlib.sha1(response.content).hexadigest()

        if sha1 not in image_hashes:
            image_hashes.append(sha1)
            image_path = "WayBackImages/%s-%s" % (sha1.image.split("/")[-1])

            with open(image_path, "wb") as fd:
                fd.write(response.content)

            self.rslBox.AppendText("[*] Saved %s" % images + "\n")
            self.rslBox.Refresh()

            info = pyexifinfo.get_json(image_path)
            info[0]['ImageHash'] = sha1
            image_results.append(info[0])

            image_results_json = json.dumps(image_results)
            data_frame = pandas.read_json(image_results_json)
            csv = data_frame.to_csv('results.csv')

    self.rslBox.AppendText("[*] Finished writing CSV to results.csv" + '\n')
    self.rslBox.Refresh()
    return


当我运行代码时,收到以下消息:

Traceback (most recent call last):
File "C:\eclipse-workspace\test\tabbedPage.py", line 136, in OnSearch
if "image" in response.headers['content-type']:
NameError: name 'response' is not defined


但是响应是在try语句中定义的-大概我会认为。它只在if“图像”部分抱怨-为什么?

我是python的新手,并且正在Eclipse中使用python3.6和pydev。
谢谢!

最佳答案

您尝试中的某些内容失败了。您的除外抓住了它来处理该错误,但是由于其中没有加价,因此它将继续执行,但是未设置响应。

关于python - Python- undefined variable ,但我认为是,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48528890/

10-13 07:32