您的程序无效语法python 2.7.15中存在错误

时间:2019-05-15 15:40:59

标签: python syntax

import urllib
import re

symbolslist = ["aaple", "spy", "goog", "nflx"]

i=0
while i<len(symbolslist):
   url = "http://finance.yahoo.com/q?s=" +symbolslist[i] +"&q1=1"
   htmlfile = urllib.urlopen(url)
   htmltext = htmlfile.read()
   regex = '<span id="yfs_184_'+symbolslist[i]'">(.+?)</span>'
   pattern = re.compile(regex)
   price = re.findall(pattern,htmltext)
   print "the price of", symbolslist[i]," is " ,price
   i+1

有人可以告诉我上述代码有什么问题吗? 谢谢。

3 个答案:

答案 0 :(得分:1)

您错过了+符号:

regex = '<span id="yfs_184_'+symbolslist[i]'">(.+?)</span>'
                                          ^
                                          |
here---------------------------------------

答案 1 :(得分:1)

使用格式可以使您的代码更具可读性https://pyformat.info/

您错过了+中的第二regex = '<span id="yfs_184_'+symbolslist[i]'">(.+?)</span>'

import urllib
import re

symbolslist = ["aaple", "spy", "goog", "nflx"]

for symbol in symbolslist:
   url = "http://finance.yahoo.com/q?s={symbol}&q1=1".format(symbol=symbol)
   htmlfile = urllib.urlopen(url)
   regex = '<span id="yfs_184_{symbol}">(.+?)</span>'.format(symbol=symbol)
   pattern = re.compile(regex)
   price = re.findall(pattern, htmlfile.read())
   print("the price of", symbol," is " ,price)

答案 2 :(得分:1)

regex = '<span id="yfs_184_'+symbolslist[i]'">(.+?)</span>'

+之后需要symbolslist[i]

regex = '<span id="yfs_184_'+symbolslist[i]+'">(.+?)</span>'