我有一个Python脚本,可以查看已经通过Beautiful Soup查找文本术语的XML文件。然后我遍历到我想要提取的值并返回字符串。这有效但因为我在程序主循环中添加了对此函数的调用,我最终得到了MemoryError问题。查看Windows任务管理器我发现每次调用后内存都没有释放,系统内存不足。问题似乎是“返回myLink.string'线。这是否包含对内存中对象的引用,如何绕过它?
def FindUptime(soup):
link = soup.find(text=re.compile("Uptime"))
if link:
myLink = link.parent
myLink = myLink.next_sibling
if myLink.string == "Up:":
myLink = myLink.next_sibling
myLink = myLink.next_element
else:
print "Can't find Up: term"
return myLink.string
else:
print "Can't find Uptime"
return ""
由
调用readings[sensorIds[i][SENSOR_NAME]] = FindUptime(soup)
答案 0 :(得分:0)
修改为......
def FindUptime(soup):
link = soup.find(text=re.compile("Uptime"))
if link:
myLink = link.parent
myLink = myLink.next_sibling
if myLink.string == "Up:":
myLink = myLink.next_sibling
myLink = myLink.next_element
else:
print "Can't find Up: term"
uptime = str(myLink.string) # added
return uptime
else:
print "Can't find Uptime"
return ""