BeautifulSoup - str 对象没有属性 findAll

BeautifulSoup - str object has no attribute findAll

我正在使用 BeautifulSoup 来抓取股票行情数据,但我 运行 使用 findAll()

遇到了问题
import urllib.request
from bs4 import BeautifulSoup
import requests
import collections

def findCSV(soupPage):
    CSV_URL_PREFIX = 'http://real-chart.finance.yahoo.com/table.csv?s='
    links = soupPage.findAll('a')
    for link in links:
        href = link.get('href', '')
        if href.startswith(CSV_URL_PREFIX):
            return href

我收到错误:

str object has no attribute findAll

我不确定是什么导致了这个问题,因为我过去在一个非常相似的实现中成功地使用了 findAll()

错误不在您提供的代码示例中:如错误消息所示,您调用了 findCSV 函数并向其传递了一个字符串。

 # you did this:
 my_string = "hello"
 findCSV(my_string)

 # instead of
 soup = BeautifulSoup('<html..></html>')
 findCSV(soup)