【问题标题】:"Unboundlocalerror: Local Variable "Val" Referenced before Assignment" Error“Unboundlocalerror:赋值前引用的局部变量“Val””错误
【发布时间】:2016-12-25 09:52:12
【问题描述】:

我一直在尝试让我的脚本以这样一种方式循环,即它将输出加载到 1 个文件中,然后当它完成加载所有内容时,将值移动到输出文件 2 中,擦除输出文件 1 中的值并开始重新加载它们,然后当它们下降时将值移动到输出二(覆盖旧的)重复。

到目前为止,我已经相当成功,不知道还有什么可以添加到我的脚本中,希望这里的人知道为什么我在中途随机出现“”Unboundlocalerror:在分配之前引用局部变量“Val”错误”加载过程中,当我有一个非常小的输入文件时,脚本会执行我想要的操作。

有谁知道我可以如何更改我的脚本来修复该错误,我试图了解它为什么会发生但不能。

我已经尝试彻底研究它,但我发现的建议都没有奏效(或者我实施不正确,我附上了我的脚本。谢谢!

    import urllib2,re,urllib,urlparse,csv,sys,time,threading,codecs,shutil
    from bs4 import BeautifulSoup


    def extract(url):
        try:
            sys.stdout.write('0')
            # global file
            page = urllib2.urlopen(url).read()

            soup = BeautifulSoup(page, 'html.parser')

            product = soup.find("div", {"class": "js-product-price"})
            price = product.findNext('div',{'class': 'js-price-display'}).getText().strip()
            oos = product.findNext('p', attrs={'class': "price-oos"})

            if oos is None:
                oos = 'In Stock'
            else:
                oos = oos.getText()

            val = url + "," + price + "," + oos + "," + time.ctime() + '\n'
            # ifile.write(val)
            sys.stdout.write('1')
        except Exception as e:
            print e

        return val

    while True:
        ifile = open('output.csv', "w", 0)
        inputs = csv.reader(open('input.csv'))
        # inputs = csv.reader(codecs.open('input.csv', 'rU', 'utf-16'))

        ifile.write('URL' + "," + 'Price' + "," + 'Stock' + "," + "Time" + '\n')

        for i in inputs:
            ifile.write(extract(i[0]))
        ifile.close()

更新:

感谢大家的帮助!这是我的新脚本:

import urllib2,re,urllib,urlparse,csv,sys,time,threading,codecs,shutil
from bs4 import BeautifulSoup


def extract(url):
    try:
        sys.stdout.write('0')
        # global file
        page = urllib2.urlopen(url).read()

        soup = BeautifulSoup(page, 'html.parser')

        product = soup.find("div", {"class": "js-product-price"})
        price = product.findNext('div',{'class': 'js-price-display'}).getText().strip()
        oos = product.findNext('p', attrs={'class': "price-oos"})

        if oos is None:
            oos = 'In Stock'
        else:
            oos = oos.getText()

        val = url + "," + price + "," + oos + "," + time.ctime() + '\n'
        # ifile.write(val)
        sys.stdout.write('1')
    except Exception as e:
        print e

    else:
        return val

while True:
    ifile = open('output.csv', "w", 0)
    inputs = csv.reader(open('input.csv'))
    # inputs = csv.reader(codecs.open('input.csv', 'rU', 'utf-16'))

    ifile.write('URL' + "," + 'Price' + "," + 'Stock' + "," + "Time" + '\n')

    for i in inputs:
        val_to_write = extract(i[0])
        if val_to_write:
            ifile.write(val_to_write)
        ifile.close()

    shutil.copy('output.csv', 'output2.csv')

print("finished")

使用上面的脚本,我现在收到错误:“ValueError: I/O operation on closed file”。谢谢

【问题讨论】:

    标签: python-2.7 loops web-scraping beautifulsoup


    【解决方案1】:

    使用try-except-else,因为如果没有引发异常,您只想使用return val(如果引发异常,那么当您尝试return 时不会分配val)。另一个建议是不要使用“catch-em-all”except 块。

    def extract(url):
        try:
            sys.stdout.write('0')
            # global file
            page = urllib2.urlopen(url).read()
    
            soup = BeautifulSoup(page, 'html.parser')
    
            product = soup.find("div", {"class": "js-product-price"})
            price = product.findNext('div',{'class': 'js-price-display'}).getText().strip()
            oos = product.findNext('p', attrs={'class': "price-oos"})
    
            if oos is None:
                oos = 'In Stock'
            else:
                oos = oos.getText()
    
            val = url + "," + price + "," + oos + "," + time.ctime() + '\n'
            # ifile.write(val)
            sys.stdout.write('1')
        except Exception as e:
            print e
    
        else:
            return val
    

    但请注意:如果确实发生异常,则 extract 将返回 None 并且调用代码必须考虑到这一点,例如:

    for i in inputs:
        val_to_write = extract(i[0])
        if val_to_write:
            ifile.write(val_to_write)
        ifile.close()
    

    【讨论】:

    • 非常感谢您的回复!!我现在正在尝试。我已经为此工作了很多年。非常感谢
    • 嘿,所以我试了一下,现在我在加载 1 或 2 个东西后收到“ValueError: I/O operation on closed file”——有什么想法吗?
    • 不要将openclose 放在while True 循环中。
    • 问这个问题我觉得很傻,但是当我尝试在脚本中删除 while true 下的开放行时,这不会导致它无法正常运行吗?
    • 我已经附上了我现在使用的脚本。不知道如何根据你说的编辑它
    猜你喜欢
    • 1970-01-01
    • 2012-11-14
    • 2013-11-29
    • 2019-09-02
    • 1970-01-01
    • 2019-11-03
    • 1970-01-01
    • 2015-06-14
    • 2018-06-29
    相关资源
    最近更新 更多