1
我的Python 3.4.4的代碼是:名稱錯誤 'HTML' 不beautifulsoup4定義
import urllib.request
from bs4 import BeautifulSoup
from html.parser import HTMLParser
urls = 'file:///C:/Users/tarunuday/Documents/scrapdata/mech.html'
htmlfile = urllib.request.urlopen(urls)
soup = BeautifulSoup(htmlfile,html.parser)
我得到這個錯誤
Traceback (most recent call last):
File "C:\Python34\saved\scrapping\scrapping2.py", line 7, in <module>
soup = BeautifulSoup(htmlfile,html.parser)
NameError: name 'html' is not defined
現在我明白了HTMLParser的是py2.x和html.parser是py3.x,但我怎麼能得到這個工作? bs4 site說If you get the ImportError 「No module named html.parser」, your problem is that you’re running the Python 3 version of the code under Python 2.
,但我運行3.x和我得到一個NameError不是一個ImportError
聖...哇人,FML。我的錯。我花了幾個小時。謝謝。 – tarunuday