取整数列表的平均值

时间:2019-01-27 18:53:17

标签: python-3.x beautifulsoup sum average

我正在抓取每日库存数量的列表,并且希望对页面的数量栏中的前20个结果取平均值。我的代码如下:

from bs4 import BeautifulSoup
import re, csv, random, time
import pandas as pd
import os
import requests

    page = requests.get('https://finance.yahoo.com/quote/BDSI/history?period1=1517033117&period2=1548569117&interval=1d&filter=history&frequency=1d')
    soup = BeautifulSoup(page.text, 'html.parser')
    rows = soup.select('table[class="W(100%) M(0)"] tr')

    for row in rows[1:20]:
        col = row.find_all("td")
        numbers = col[6].text.replace(',', '')
        numbers2 = int(numbers)
        print(numbers2)

        avg20vol = sum(numbers2(1,20))/len(numbers2)

...但是尝试获取返回数字的平均值时我陷入困境。用我尝试过的解决方案接收“ TypeError:'int'对象不可调用”或“ TypeError:'int'对象不可迭代”。如何处理平均列表?是否涉及先将其转换为数据帧?谢谢!

更新

这是适用代码段的有效示例:

    numberslist=[]

    for row in rows[1:21]:
        col = row.find_all("td")
        numbers = col[6].text.replace(',', '')
        numbers2 = int(numbers)
        numberslist.append(numbers2)
        print(numbers2)
        average = sum(numberslist)/len(numberslist)


    print('Average = ',average)

2 个答案:

答案 0 :(得分:1)

在抓取时,实际上要创建一个数字列表,如下所示:

# stuff before

number_list = []  # empty list

for row in rows[1:20]:
    # get the number
    number_list.append(int(number_as_string))  # add the new number at the end of the list

average = sum(number_list)/len(number_list)

您还可以.append()字符串形式,然后使用intlist(map(int(list_of_strings))转换为[int(x) for x in list_of_strings]

注意:rows[1:20]将省略第一项,正如您所说的,第一行是标题。使用rows[:20]可以大致获取前20个项目。

答案 1 :(得分:1)

您的CSS选择器也是错误的,给了我一个错误。

from bs4 import BeautifulSoup
import requests
page = requests.get('https://finance.yahoo.com/quote/BDSI/history?period1=1517033117&period2=1548569117&interval=1d&filter=history&frequency=1d')
soup = BeautifulSoup(page.text, 'html.parser')
rows = soup.find('table',class_="W(100%) M(0)").find_all('tr')
numbers=[]
for row in rows[1:20]:
    col = row.find_all("td")
    print(col[6].text)
    number = col[6].text.replace(',', '')
    number = int(number)
    numbers.append(number)

avg20vol =sum(numbers)/len(numbers)
print("Average: ",avg20vol)

输出

650,100
370,500
374,700
500,700
452,500
1,401,800
2,071,200
1,005,800
441,500
757,000
901,200
563,400
1,457,000
637,100
692,700
725,000
709,000
1,155,500
496,400
Average:  808584.2105263158