使用 Python 进行 Web 抓取:超出 RuntimeError 最大递归深度

Web Scraping with Python: RuntimeError maximum recursion depth exceeded

我正试图从一个经常更新的网站上提取一些数据(这里我使用的是雅虎财经的 AAPL 股票)。此代码不 运行。我已经单独测试了每个部分,但我仍然收到一条错误消息 "RuntimeError: maximum recursion depth exceeded".
预先感谢您帮助解决问题![​​=13=]

import time  
import lxml, requests  
from bs4 import BeautifulSoup  

url= "http://finance.yahoo.com/q?uhb=uh3_finance_vert&fr=&type=2button&s=aapl"  

def PrintPriceAAPL():  
    r = requests.get(url)  
    soup = BeautifulSoup(r.content, "lxml")  
    print (soup.find(id="yfs_l84_aapl").string) #ID of current stock price  
    time.sleep(60)  

while True:  
    PrintPriceAAPL()  

编辑: 这是完整的错误:

Traceback (most recent call last):
  File "C:/Users/sjung/Documents/PrintPriceAAPL.py", line 15, in <module>
    PrintPriceAAPL()
  File "C:/Users/sjung/Documents/PrintPriceAAPL.py", line 11, in PrintPriceAAPL
    print (soup.find_all(id="yfs_l84_aapl")[0].string) #ID of current stock price
  File "C:\Python27\lib\idlelib\rpc.py", line 595, in __call__
    value = self.sockio.remotecall(self.oid, self.name, args, kwargs)
  File "C:\Python27\lib\idlelib\rpc.py", line 210, in remotecall
    seq = self.asynccall(oid, methodname, args, kwargs)
  File "C:\Python27\lib\idlelib\rpc.py", line 225, in asynccall
    self.putmessage((seq, request))
  File "C:\Python27\lib\idlelib\rpc.py", line 324, in putmessage
    s = pickle.dumps(message)
  File "C:\Python27\lib\copy_reg.py", line 74, in _reduce_ex
    getstate = self.__getstate__
RuntimeError: maximum recursion depth exceeded

find() 递归搜索 children。因此,find() 将查看 soup 的所有后代:它的 children、childrens' children 等

每次检查 children 的下一层时,run-time 堆栈的 "recursion depth" 都会增加。 如果您只对直接 children 感兴趣,find() 有一个 recursive=False 参数,它应该只检查下一级。

或者,您可以增加递归深度,但要小心。参考此 post:What is the maximum recursion depth in Python, and how to increase it?

有关 run-time 堆栈的更多信息,请查看:https://chortle.ccsu.edu/AssemblyTutorial/Chapter-25/ass25_9.html