所需数据:
我想浏览两个网页,一个在这里:https://finance.yahoo.com/quote/AAPL/balance-sheet?p=AAPL,另一个:https://finance.yahoo.com/quote/AAPL/financials?p=AAPL。 从第一页开始,我需要名为总资产的行的值。该行中有5个值,名为:365725000375319000 321686000 290479000 231839000 然后我需要5个名为总流动负债的行的值。这将是:43658000 38542000 2797000207220011506000 在第二个链接中,我需要10个名为营业收入或亏损的行的值。这将是:52503000 48999000 55241000 33790000 18385000。在
还需要编辑5年以上的值。谢谢。 这是我想要什么的逻辑。我想运行这个模块,当运行时,我希望输出是:
TTM array: 365725000, 116866000, 64423000
year1 array: 375319000, 100814000, 70898000
year2 array: 321686000, 79006000, 80610000
我的代码:
这是我迄今为止所写的。如果我把它放在下面的变量中,我可以把它放在类中。但是,如何有效地循环使用'div'类,因为页面中有数千个类。换句话说,我怎样才能找到我想要的价值观呢?在
^{pr2}$
更新
原始回复
这是另一个答案,可能会由比我更擅长做汤的人来完善。在
我把收集到的数据放入两个字典中,分别命名为资产负债表_dict和金融学。我还提取了与列关联的日期,因为我将在其他函数中使用它们。我还将这些日期从%m/%d/%Y重新格式化为%m%d%Y
我还使用soups find_all_next(tag_name,limit=int)只收集所需的子标记。您可以调整此限制以从表中收集所需的项。在
总的来说,这是一个有趣的问题,需要一些额外的思考。谢谢你发帖提问。在
请参考您对查询2016年名为资产负债表_dict的字典的评论:
^{pr2}$一些关于解析
html
的建议使用'beauthoulsoup',这对我很有帮助也许对你也有帮助。在下面是针对您的需求的可运行代码使用结构信息定位元素。你绝对可以使用“类”信息来制作它。只要记住,当你的代码不能正常工作时,请检查网站的源代码。在
带标题信息的输出:
^{pr2}$编辑-应@Life的要求是复杂的,编辑后添加日期标题。在
使用lxml试试这个:
输出:
^{pr2}$当然,如果需要,可以很容易地将其合并到pandas数据帧中。在
相关问题 更多 >
编程相关推荐