2017-07-17 6 views
1
Traceback (most recent call last): 
    File "bs4.py", line 1, in <module> 
    import bs4 
    File "/home/mhadi/Desktop/bs4test/bs4.py", line 5, in <module> 
    soup = bs4.BeautifulSoup(site,'lxml') 
AttributeError: module 'bs4' has no attribute 'BeautifulSoup' 

Der Code:Attribute: Modul 'BS4' hat kein Attribut 'BeautifulSoup'

import bs4 
import urllib.request 

site = urllib.request.urlopen('http://127.0.0.1:8000').read() 
soup = bs4.BeautifulSoup(site,'lxml') 
#for i in site: 
# print(site[i]) 
print(soup) 
+0

'von BS4 Import BeautifulSoup' und dann direkt' verwenden BeautifulSoup' – rednaks

+1

Ich glaube, Sie nicht Ihre Datei 'bs4' nennen sollten weil es Verwirrung verursacht. –

Antwort

1

Das Problem ist, dass Ihr Dateiname bs4.py ist. Wenn Sie jetzt eine import-Anweisung schreiben, sucht Python zuerst nach lokalen Dateien mit diesem Namen. Es wird also davon ausgegangen, dass sich Ihre import bs4 auf Ihre eigene Datei bezieht. Ihre Datei wird sich daher selbst importieren, enthält aber offensichtlich nicht das gewünschte Modul.

Eine schnelle Lösung ist die Datei umbenennen. Zum Beispiel in bs4tests.py. Dann können Sie import bs4 verwenden.

Alternativ können Sie zum Beispiel versuchen, den lokalen Pfad zu entfernen, wie:

import sys    # import sys package 
old_path = sys.path[:] # make a copy of the old paths 
sys.path.pop(0)   # remove the first one (usually the local) 
import bs4    # import the package 
sys.path = old_path  # restore the import path 
+0

sein gelöst thanx –

Verwandte Themen