faizanm17
faizanm17

Reputation: 9

Getting 'AttributeError: type object 'BeautifulSoup' has no attribute 'BeautifulSoup' in python code

I have imported bs4 as such from bs4 import BeautifulSoup as bs. Here is the part where I initialize beautiful soup in my code:

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

When I run my script via bash, I get the following error:

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'

Not sure what the attribute error is referring to. Also, when I run my code in an jupyter notebook it works fine.

Upvotes: 0

Views: 1234

Answers (1)

ForceBru
ForceBru

Reputation: 44838

If you drop the as bs part, your code will be equivalent to:

from bs4 import BeautifulSoup

soup = BeautifulSoup.BeautifulSoup(resp.text)

And the error message says that BeautifulSoup has no attribute BeautifulSoup.

Did you mean this?

import bs4

soup = bs4.BeautifulSoup(resp.text)

Or maybe:

from bs4 import BeautifulSoup as bs

soup = bs(resp.text)

Upvotes: 2

Related Questions