Python scraping from Yahoo finance error: 'NoneType' object has no attribute 'parent'

Python scraping from Yahoo finance error: 'NoneType' object has no attribute 'parent'

我正在尝试使用 Python 从 Yahoo Finance 的损益表中抓取数据。

我想提取 净收入,它包含在:

import re, requests
from bs4 import BeautifulSoup

url = 'https://finance.yahoo.com/q/is?s=AAPL&annual'
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
pattern = re.compile('Net Income')

title = soup.find('strong', text=pattern)
row = title.parent.parent 
cells = row.find_all('td')[1:] #exclude the <td> with 'Net Income'

values = [ c.text.strip() for c in cells ]

但是我收到了这个错误:

您知道导致此问题的原因吗?

您可以通过搜索 'div' 标签来获取净收入值。这应该可以解决问题:

import re, requests
from bs4 import BeautifulSoup

url = 'https://finance.yahoo.com/q/is?s=AAPL&annual'
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')

title = soup.find('div', string=re.compile('Net Income'))
row = title.parent.parent 
values = [i.text for i in row]
print(values[1:])

结果:

['57,215,000', '55,256,000', '59,531,000', '48,351,000', '45,687,000']