Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 &引用;Unboundlocalerror:局部变量;Val";作业前参考“;错误_Python 2.7_Loops_Web Scraping_Beautifulsoup - Fatal编程技术网

Python 2.7 &引用;Unboundlocalerror:局部变量;Val";作业前参考“;错误

Python 2.7 &引用;Unboundlocalerror:局部变量;Val";作业前参考“;错误,python-2.7,loops,web-scraping,beautifulsoup,Python 2.7,Loops,Web Scraping,Beautifulsoup,我一直在尝试让我的脚本以这样的方式循环,它将加载1个文件中的输出,然后在加载完所有内容后将值移动到输出文件2中,擦除输出文件1中的值并开始重新加载它们,然后当这些值向下移动时,将值移动到输出2中(覆盖旧的值)重复 到目前为止,我已经非常成功了,不知道还有什么可以添加到我的脚本中,我希望这里的人知道为什么我在加载过程的中途不断出现“Unboundlocalerror:localvariable“Val”Referenced before Assignment”错误,当我有一个非常小的输入文件时,脚

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

到目前为止,我已经非常成功了,不知道还有什么可以添加到我的脚本中,我希望这里的人知道为什么我在加载过程的中途不断出现“Unboundlocalerror:localvariable“Val”Referenced before Assignment”错误,当我有一个非常小的输入文件时,脚本会按照我的要求执行

有人知道我如何修改脚本来修复这个错误吗?我已经试着理解为什么会发生这个错误,但我做不到

我试图彻底研究它,但我发现没有一个建议是有效的(或者我没有正确地执行它们,我附上了我的脚本。谢谢

    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操作”。感谢使用
try except else
,因为如果没有引发异常,您只想
返回val
(如果引发了异常,那么当您尝试
返回
它时,将不会分配
val
)。另一个建议是不要使用“catch em all”
“catch em all”
“catch em all”块

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

非常感谢您的回复!!我现在正在尝试。我已经在这方面工作了很多年。非常感谢嘿,所以我尝试了一下,现在我得到了“ValueError:I/O operation on closed file”"在加载1或2件事情之后——有什么想法吗?不要
打开
关闭
while True
循环中的文件。问这个问题我觉得很傻,但是当我尝试删除脚本while True下的打开行时,这不会导致它无法正常工作吗?我已经附加了我现在使用的脚本。不确定如何编辑它根据你说的