python - 从雅虎财务错误中抓取 Python:“NoneType”对象没有属性“父”
问题描述
我正在尝试使用 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']
推荐阅读
- r - 使用 R 从 SEC 抓取 13F 文件
- ruby-on-rails - 如何重用 Rails 中 lib 模块内的帮助程序中的方法?
- authentication - 使用用户别名登录的 Okta 配置
- javascript - 如何使用 OpenLayers 6.5 示例?
- responsive-design - Arrow SVG - 添加额外的线
- python - 获取开始日期和结束日期熊猫列之间的所有日期
- amazon-web-services - 实例的 AWS OpsWorks setup_failed - 无法部署_branch
- c# - 使用嵌套循环,我想将我的数据表填充到另一个数据表中
- python - 通过我的代码运行 GIF 时,如何解决此属性错误?
- php - 如何使用带有 get_the_content() 的 DOMDocument 从 Wordpress 帖子中删除包含某些文本字符串的多个 HTML 标记