【问题标题】:'ResultSet' object has no attribute 'find_all''ResultSet' 对象没有属性 'find_all'
【发布时间】:2015-06-24 21:01:13
【问题描述】:

我在下面有一个函数,可以解析来自 Yahoo Finance 的数据,然后写入 cvs 文件。 data_scrapper 返回一个带有 symbol 的纯 html。 我收到一个错误:

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

【问题讨论】:

  • 您的请求可能有误...

标签: python django beautifulsoup


【解决方案1】:
table = soup.findAll('table') # Here is set of tables

你需要遍历表(ResultSet)

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

【讨论】:

    猜你喜欢
    • 2018-08-06
    • 2016-12-19
    • 2013-03-30
    • 2017-04-25
    • 1970-01-01
    • 2016-08-16
    • 2014-07-29
    • 2019-06-20
    • 1970-01-01
    相关资源
    最近更新 更多