无法从“ bs4”导入名称“ BeautifulSoup”

时间:2019-07-25 10:02:06

标签: python-3.x runtime-error

我正在使用Python 3.7.0 我无法使用bs4的BeautifulSoup属性

我已重新安装该库,但仍无法正常工作。

from bs4 import BeautifulSoup
import csv
html = open("table.html").read()
soup = BeautifulSoup(html)
table = soup.find("table")

output_rows = []
for table_row in table.findAll('tr'):
    columns = table_row.findAll('td')
    output_row = []
    for column in columns:
        output_row.append(column.text)
    output_rows.append(output_row)

with open('output.csv', 'wb') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerows(output_rows)

它抛出以下错误:

Traceback (most recent call last):
  File "D:\Automation\ReadHtml\html.py", line 1, in <module>
    from bs4 import BeautifulSoup
  File "D:\Python37\lib\site-packages\bs4\__init__.py", line 34, in <module>
    from .builder import builder_registry, ParserRejectedMarkup
  File "D:\Python37\lib\site-packages\bs4\builder\__init__.py", line 7, in <module>
    from bs4.element import (
  File "D:\Python37\lib\site-packages\bs4\element.py", line 19, in <module>
    from bs4.formatter import (
  File "D:\Python37\lib\site-packages\bs4\formatter.py", line 1, in <module>
    from bs4.dammit import EntitySubstitution
  File "D:\Python37\lib\site-packages\bs4\dammit.py", line 13, in <module>
    from html.entities import codepoint2name
  File "D:\Automation\ReadHtml\html.py", line 1, in <module>
    from bs4 import BeautifulSoup
ImportError: cannot import name 'BeautifulSoup' from 'bs4' (D:\Python37\lib\site-packages\bs4\__init__.py)

编辑:

我使用

安装
pip install BeautifulSoup4

也已安装

pip install bs4

但它仍然无法正常工作。

1 个答案:

答案 0 :(得分:0)

保存完毕,谢谢,刚刚将文件从html.py重命名为something.py