【问题标题】:name error 'html' not defined with beautifulsoup4名称错误“html”未使用 beautifulsoup4 定义
【发布时间】:2016-03-20 12:39:42
【问题描述】:

我的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 siteIf 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

【问题讨论】:

    标签: python html python-3.x beautifulsoup


    【解决方案1】:

    错误是正确的,你没有在任何地方定义html。您链接到的文档显示您应该将"html.parser" 作为字符串传递;看起来您根本不需要导入 HTMLParser。

    【讨论】:

    • 圣洁...哇,FML。我的错。我为此花了几个小时。谢谢。
    【解决方案2】:

    在您的代码中,html.parser 是一个字符串,如果您使用的是 python 3 或更高版本,则需要在其周围加上引号。

    【讨论】:

      猜你喜欢
      • 2022-01-01
      • 1970-01-01
      • 2015-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多