更新:为了回应 Wooble 的评论,在 for 之前添加一个“sector = None”只会返回“None”。我认为问题在于 for 循环中的变量没有被返回。
以下是运行良好的函数的一部分,直到最近,当我更改了代码中看似无关的部分时。
#--> 我最近更改的唯一部分是在 return 语句中添加“stockurl”
我现在得到UnboundLocalError: local variable "sector" referenced before assignment,指的是“return”行
for sec in root.xpath(r'''//a[re:match(@href, "http://biz.yahoo.com/p/[0-9]{1}conameu.html")]''',
namespaces={'re': 'http://exslt.org/regular-expressions'}):
sector = sec.text
#print "Sector: " + sector
for ind in root.xpath(r'''//a[re:match(@href, "http://biz.yahoo.com/ic/[0-9]{1,9}.html")]''',
namespaces={'re': 'http://exslt.org/regular-expressions'}):
industry = ind.text
#print "Industry: " + industry
#index --> don't populate here
#followers --> don't populate here
return a, b, c, d, e, f, stockurl, sector, industry
#--> the only part I had changed recently was adding "stockurl" to the function