从函数返回时,DataFrame数据出现两次

我目前有一个将从条目列表中获取数据的函数。我有一个DataFrame,它将重复数据。我的代码是

import pandas as pd
import numpy as np
import csv
from requests import get
from bs4 import BeautifulSoup

def earnings(ticker):
    url_test = ('https://finance.yahoo.com/calendar/earnings?from=2018-04-08&to=2018-04-14&day=2018-04-12&symbol={}')
    for i in l:
        url = url_test.format(i)
        response = get(url)
        soup = BeautifulSoup(response.text, 'html.parser')
        table = soup.find('table',{'class':'W(100%)'})

        for tr in table:
            tr.find('tr')
            for td in tr:
                td.find('td')
                if len(td.contents) >= 3:
                    tic = td.contents[0].text
                    tic_list.append(tic)
                    tic_df = pd.DataFrame(tic_list)
                    dates = td.contents[2].text
                    dates_list.append(dates)
                    dates_df = pd.DataFrame(dates_list)
                    earnings_df = pd.concat( [dates_df, tic_df], axis=1)
    return(earnings_df)

l = {"AAPL", "KO"}
for ticker in l:
    earnings_df = earnings(ticker)
    earnings_df.to_csv('earnings_df.csv')

When looking at my csv file I will get the same data twice in the form of it repeating under itself. I feel like I messed up somewhere when creating the earnings_df. Any ideas?

评论