我的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)
我收到这个错误
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网站说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
该错误是正确的,您尚未html
在任何地方定义。链接到的文档表明您应该以"html.parser"
字符串形式进行传递。它看起来根本不需要导入HTMLParser。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句