BeautifulSoup
BeautifulSoup - str对象没有属性findAll(BeautifulSoup - str object has no attribute findAll)我正在使用BeautifulSoup来抓取股票代码数据,但我遇到了使用findAll()的问题
import urllib.requestfrom bs4 import BeautifulSoupimport requestsimport collectionsdef findCSV(soupPage): CSV_URL_PREFIX = '.csv?s=' links = soupPage.findAll('a') for link in links: href = link.get('href', '') if href.startswith(CSV_URL_PREFIX): return href我收到错误:
str对象没有属性findAll
我不确定是什么导致了这个问题,因为我过去在一个非常类似的实现中成功使用了findAll() 。
I'm using BeautifulSoup to crawl stock ticker data, but I'm running into an issue using findAll()
import urllib.requestfrom bs4 import BeautifulSoupimport requestsimport collectionsdef findCSV(soupPage): CSV_URL_PREFIX = '.csv?s=' links = soupPage.findAll('a') for link in links: href = link.get('href', '') if href.startswith(CSV_URL_PREFIX): return hrefI get the error:
str object has no attribute findAll
I'm not sure what's causing this problem, because I have used findAll() successfully in the past in a very similar implementation.
最满意答案该错误不在您提供的代码示例中:如错误消息所示,您已调用findCSV函数findCSV传递字符串。
# you did this: my_string = "hello" findCSV(my_string) # instead of soup = BeautifulSoup('<html..></html>') findCSV(soup)The bug is not in the code sample that you have provided: as the error message indicates, you have called the findCSV function passing it a string.
# you did this: my_string = "hello" findCSV(my_string) # instead of soup = BeautifulSoup('<html..></html>') findCSV(soup)