" Unboundlocalerror:Local Variable" Val"在作业之前引用"错误

时间:2016-12-25 09:52:12

标签: python-2.7 loops web-scraping beautifulsoup

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

到目前为止,我已经取得了相当大的成功,并且不知道还有什么可以添加到我的脚本中,我希望有人知道为什么我会继续获取"" Unboundlocalerror:Local Variable" VAL"在作业之前引用"在加载过程中间随机出错,当我有一个非常小的输入文件时,脚本会执行我想要的方式。

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

我试图彻底研究它,但我找到的建议都没有奏效(或者我错误地实施了它们,我附上了我的脚本。谢谢!

data.ContainsValue(sub)

更新

感谢帮帮!这是我的新剧本:

    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()

使用上面的脚本我现在收到错误:" ValueError:关闭文件的I / O操作"。谢谢

1 个答案:

答案 0 :(得分:0)

如果没有引发异常,请使用majority_vp_fk = set(majority_df.vp_fk) minority_vp_fk = set(minority_df.vp_fk) clean_majority_vp_fk = majority_vp_fk - minority_vp_fk clean_majority_df = majority_df[majority_df.vp_fk.isin(clean_majority_vp_fk)] clean_majority_df = clean_majority_df.drop_duplicates(subset=['probe_fk', 'vp_fk', 'masking_box_fk', 'product_fk']) try-except-else {如果引发了异常,那么当您尝试{return val时,val将不会被分配给return {1}}它)。另一个建议是不要使用" 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()