我的 python 3.4.4 代码是:
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)
Run Code Online (Sandbox Code Playgroud)
我收到这个错误
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
Run Code Online (Sandbox Code Playgroud)
现在我明白 HTMLParser 是 py2.x 和 html.parser 是 py3.x 但我怎样才能让它工作?该BS4网站说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. …