2015-06-24 37 views
0

我有一個函數,它解析來自Yahoo Finance的數據,然後寫入一個cvs文件。 data_scrapper返回一個純html提供的symbol。 我得到了一個錯誤:'ResultSet'對象沒有屬性'find_all'

Exception Value: 'ResultSet' object has no attribute 'find_all' 
Exception Location: ./finance_parser/views.py in button_clicked, line 76 

76號線是指這條線:headers = [header.text for header in table.find_all('th')]

def button_clicked(request): 

    rows = [] 
    current_url = request.META['HTTP_REFERER'] 
    symbol = current_url[-4:-1].replace('%20', ' ') 
    gen_table = data_scrapper(symbol) 

    soup = BeautifulSoup(gen_table) 
    table = soup.findAll('table') 
    headers = [header.text for header in table.find_all('th')] 
    for row in table.findAll('tr'): 
     rows.append([val.text for val in row.findAll('td')]) 

    response = HttpResponse(content_type='text/csv') 
    response['Content-Disposition'] = 'attachment; filename= "{}.csv"'.format(symbol) 

    writer = csv.writer(response) 
    writer.writerow(headers) 
    writer.writerows(row for row in rows if row) 

    return response 
+0

你的要求可能是錯誤的... –

回答

2
table = soup.findAll('table') # Here is set of tables 

您需要通過表迭代(結果集)

headers = [] 
for table in soup.findAll('table'): 
    headers.extend([header.text for header in table.find_all('th')]) 
相關問題