如何将使用BeatifulSoup4获得的数据作为浮点或整数赋给变量?

2024-04-19 01:55:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python新手,很抱歉提出这样一个noob问题,但我对以下代码有两个问题:

1)我正在尝试从网站中获取数据,但在通过for循环以外的任何方法获取数据时遇到问题,因此我需要继续使用print循环,以便能够在以下命令中将值赋给变量:

receivables2017 = receivables2017.string

2)接下来,我尝试将特定值(例如Receivables2017)赋给aapl字典,但无法以floatint的形式获取字典中的值,这需要稍后应用于方程。你知道吗

非常感谢您的帮助,谢谢!你知道吗

import bs4 as bs
import urllib.request


sauce = urllib.request.urlopen('https://www.zacks.com/stock/quote/AAPL/balance-sheet')
soup = bs.BeautifulSoup(sauce,'lxml')

#Cash & Cash Equivalents
for cash_and_equivalents2017 in soup.find_all('td')[33]:
    print(cash_and_equivalents2017.string)

cash_and_equivalents2017 = cash_and_equivalents2017.string

for cash_and_equivalents2016 in soup.find_all('td')[34]:
print(cash_and_equivalents2016.string)

cash_and_equivalents2016 = cash_and_equivalents2016.string

#Receivables
for receivables2017 in soup.find_all('td')[39]:
    print(receivables2017.string)

receivables2017 = receivables2017.string

for receivables2016 in soup.find_all('td')[40]:
    print(receivables2016.string)

receivables2016 = receivables2016.string



aapl = {'Cash & Cash Equivalents':
            {'2017': cash_and_equivalents2017,
             '2016': cash_and_equivalents2016},
        'Receivables':
            {'2017': receivables2017,
             '2016': receivables2016}
        {

print(aapl)

Tags: andinforstringcashallfindtd
2条回答

您不需要for循环将值赋给变量:

for receivables2016 in soup.find_all('td')[40]:
    print(receivables2016.string)

可缩短为:

receivables2016 = soup.find_all('td')[40].string

如果您尝试迭代soup的元素,即使它只包含一个元素,它也确实会生成一个对象。这就是它的设计原理。但它实际上是一个包含一些数据的元素,不一定是iterable(list、tuple等)。 您应该看看python的迭代器和生成器: https://anandology.com/python-practice-book/iterators.html#the-iteration-protocol

如果您不想让代码块做任何事情,只需使用pass

for i in range(10):
    pass # will iterate 10 times but won't don anything

或者如果您定义了一个函数,但尚未决定实现:

def my_funct():
    pass

如果my_funct被调用,它将在默认情况下返回None,因为没有声明返回值。你知道吗

我看到您希望在dict中包含的值的小数点是,。如果它是一个字符串,您可以很容易地将逗号替换为一个点,然后将其转换为浮点数:

aapl = {'Cash & Cash Equivalents':
            {'2017': float(cash_and_equivalents2017.replace(",", ".")),
             '2016': float(cash_and_equivalents2016.replace(",", "."))},
        'Receivables':
            {'2017': float(receivables2017.replace(",", ".")),
             '2016': float(receivables2016.replace(",", "."))}
        }

使用python内置的int()函数,可以将数字字符串转换为整数。请记住,即使是字符串格式,它们也必须是有效整数。这不起作用,会抛出ValueError

int(receivables2016.replace(",", "."))

在这种情况下,必须将字符串转换为float,然后再转换为整数:

int(float(receivables2016.replace(",", ".")))

我不确定这是不是你要找的,但是:

aapl = dict([('Cash & Cash Equivalents 2017', cash_and_equivalents2017),
         ('Cash & Cash Equivalents 2016', cash_and_equivalents2016),
         ('Receivables 2017', receivables2017),
         ('Receivables 2016', receivables2016)])

print(aapl)

为我回来工作:

{'Cash & Cash Equivalents 2016': '67,155', 'Receivables 2016': '29,299', 'Cash & Cash Equivalents 2017': '74,181', 'Receivables 2017': '35,673'}

相关问题 更多 >