获取'AttributeError:type对象'BeautifulSoup'在python代码中没有属性'BeautifulSoup'

2024-09-28 05:21:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经导入了bs4 from bs4 import BeautifulSoup as bs。下面是我在代码中初始化beautiful soup的部分:

def save_sp500_tickers():
    resp = requests.get('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')
    soup = bs.BeautifulSoup(resp.text)

通过bash运行脚本时,出现以下错误:

16:27 ~ $ python2.7 toptickerbot.py
Traceback (most recent call last):
  File "toptickerbot.py", line 332, in <module>
    result()
  File "toptickerbot.py", line 209, in result
    data = scandata(30000000, 200000000, -0.99, 0.99, 0, 25)
  File "toptickerbot.py", line 149, in scandata
    tickers = save_sp500_tickers()
  File "toptickerbot.py", line 26, in save_sp500_tickers
    soup = bs.BeautifulSoup(resp.text)
AttributeError: type object 'BeautifulSoup' has no attribute 'BeautifulSoup'

不确定属性错误指的是什么。此外,当我在jupyter笔记本中运行代码时,它工作正常


Tags: 代码textinpybssavelineresp
1条回答
网友
1楼 · 发布于 2024-09-28 05:21:27

如果删除as bs部分,代码将等效于:

from bs4 import BeautifulSoup

soup = BeautifulSoup.BeautifulSoup(resp.text)

错误消息说BeautifulSoup没有属性BeautifulSoup

你是说这个吗

import bs4

soup = bs4.BeautifulSoup(resp.text)

或者可能:

from bs4 import BeautifulSoup as bs

soup = bs(resp.text)

相关问题 更多 >

    热门问题