【发布时间】:2021-12-27 10:13:40
【问题描述】:
我正在制作一个网络爬虫,它可以抓取雅虎财经并告诉我当前的股价是多少。
运行程序后我不断收到这样的错误
IndexError: list index out of range
这是代码
def parsePrice():
r=requests.get('https://finance.yahoo.com/quote/F?p=F')
soup=bs4.BeautifulSoup(r.text,'xml')
#the next line is the supposed problem
price=soup.find_all('div',{'class': 'My(6px) Pos(r) smartphone_Mt(6px)'})[0].Find('span').text
return price
while True:
print('the current price is: '+str(parsePrice()))
我是 python 的初学者,所以任何帮助将不胜感激:)
【问题讨论】:
标签: python web-scraping beautifulsoup