Py学习  »  Python

迭代加法的Python语法错误,例如x+=1[关闭]

bio_inf_dreamer • 4 年前 • 250 次点击  

为Finviz构建股票数据刮刀。我正在使用Spyder3,它是通过Anaconda for Python 3.7安装的。

我的代码在下面。当我在终端中逐行执行x=0,然后x=x+1代码时,它工作得很好。当我运行整个脚本时,如果使用x+=1或x=x+1,我会得到相同的错误。

def finviz_query(tickerlist):

'''Get's source code from FinViz and Creates a List of Lists for Export '''
url="https://finviz.com/screener.ashx?v=140&t=" + str(stocks)
response = requests.get(url)
source=response.text
soup = bs4.BeautifulSoup(source)
priceLST = [i.get_text() for i in soup.find_all('a')]
del priceLST[0:37]
del priceLST[len(priceLST)-2:len(priceLST)]
stockLST = re.split(',',stocks)
stock_outputLST = []
while len(priceLST) > 0:
    if priceLST[0] in stockLST:
        stock_outputLST.append([priceLST[0:16]])
        del priceLST[0:16]
    if len(priceLST) < 1:
        break
x = 0
while x < len(stock_outputLST):
    if x < len(stock_outputLST):
        stock_outputLST[x][0].append(time.strftime("%Y-%m-%d;%H:%M")
        x = x + 1
    else:
        break
stock_outputLST[len(stock_outputLST)-1][0].append('0')
stock_outputLST[len(stock_outputLST)-1][0].append(time.strftime("%Y-%m-%d;%H:%M"))

错误输出如下:

...:stock_outputLST[len(stock_outputLST)-1][0].append(time.strftime("%Y-%m-%d;%H:%M"))
...: return print('finviz_query complete')
  File "<ipython-input-114-81b306b1a6de>", line 22
    x = x + 1
    ^
SyntaxError: invalid syntax

提前谢谢!

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/49329
 
250 次点击  
文章 [ 1 ]  |  最新文章 4 年前
Greg Hewgill
Reply   •   1 楼
Greg Hewgill    5 年前

每当你遇到这样一个不太合理的错误时,请看上面的一行:

    stock_outputLST[x][0].append(time.strftime("%Y-%m-%d;%H:%M")

你缺少右括号 ) 最后。