【发布时间】:2016-11-30 23:09:48
【问题描述】:
我的问题是here 的后续问题。
功能:
periodic_figure_values()
似乎工作正常,除非正在搜索的订单项的名称出现两次。我所指的具体案例是试图获取“长期债务”的数据。上面链接中的函数会返回如下错误:
Traceback (most recent call last):
File "test.py", line 31, in <module>
LongTermDebt=(periodic_figure_values(soup, "Long Term Debt"))
File "test.py", line 21, in periodic_figure_values
value = int(str_value)
ValueError: invalid literal for int() with base 10: 'Short/Current Long Term Debt'
因为它似乎被“短期/当前长期债务”绊倒了。您会看到,该页面同时具有“短期/当前长期债务”和“长期债务”。您可以查看使用 Apple 资产负债表 here 的源页面示例。
我正在尝试为函数找到一种方法来返回“长期债务”的数据,而不会被“短期/当前长期债务”绊倒。
以下是获取“现金和现金等价物”的函数和示例,它可以正常工作,而“长期债务”则不能正常工作:
import requests, bs4, re
def periodic_figure_values(soup, yahoo_figure):
values = []
pattern = re.compile(yahoo_figure)
title = soup.find("strong", text=pattern) # works for the figures printed in bold
if title:
row = title.parent.parent
else:
title = soup.find("td", text=pattern) # works for any other available figure
if title:
row = title.parent
else:
sys.exit("Invalid figure '" + yahoo_figure + "' passed.")
cells = row.find_all("td")[1:] # exclude the <td> with figure name
for cell in cells:
if cell.text.strip() != yahoo_figure: # needed because some figures are indented
str_value = cell.text.strip().replace(",", "").replace("(", "-").replace(")", "")
if str_value == "-":
str_value = 0
value = int(str_value)
values.append(value)
return values
res = requests.get('https://ca.finance.yahoo.com/q/bs?s=AAPL')
res.raise_for_status
soup = bs4.BeautifulSoup(res.text, 'html.parser')
Cash=(periodic_figure_values(soup, "Cash And Cash Equivalents"))
print(Cash)
LongTermDebt=(periodic_figure_values(soup, "Long Term Debt"))
print(LongTermDebt)
【问题讨论】:
标签: python regex web-scraping beautifulsoup python-requests