Cannot figure out what's wrong with beautifulsoup4 in my python 3 script

风格不统一 提交于 2019-12-24 06:47:50

问题


Traceback (most recent call last):   File "urlgrabber.py", line 1, in <module>
    from bs4 import BeautifulSoup   File "/Users/asdf/Desktop/Scraper/bs4/__init__.py", line 29, in <module>
    from .builder import builder_registry   File "/Users/asdf/Desktop/Scraper/bs4/builder/__init__.py", line 4, in <module>
    from bs4.element import (   File "/Users/asdf/Desktop/Scraper/bs4/element.py", line 5, in <module>
    from bs4.dammit import EntitySubstitution   File "/Users/asdf/Desktop/Scraper/bs4/dammit.py", line 13, in <module>
    import logging   File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/logging/__init__.py", line 28, in <module>
    from string import Template ImportError: cannot import name 'Template'

I have code that basically scrapes link off of a website. It was working fine till recently it just started returning this error on the command prompt. I am using python 3. I have downloaded bs4 and updated it along with python. I am still getting this error. Still new to Python/programming and am not sure how to fix this. I put the command prompt's messages up above. Code before was standard scraping code that worked fine but for some reason as of recently was not working. I can have 'from bs4 import BeautifulSoup' on a new .py file and it'll give me the same error:

File "bs4fml.py", line 1, in <module>
    from bs4 import BeautifulSoup
  File "/Users/asdf/Desktop/Scraper/bs4/__init__.py", line 29, in <module>
    from .builder import builder_registry
  File "/Users/asdf/Desktop/Scraper/bs4/builder/__init__.py", line 4, in <module>
    from bs4.element import (
  File "/Users/asdf/Desktop/Scraper/bs4/element.py", line 5, in <module>
    from bs4.dammit import EntitySubstitution
  File "/Users/asdf/Desktop/Scraper/bs4/dammit.py", line 13, in <module>
    import logging
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/logging/__init__.py", line 28, in <module>
    from string import Template

回答1:


I searched your error message and came up with this: https://bbs.archlinux.org/viewtopic.php?id=142036

It looks like the problem is that Python is trying to import from string from a local source instead of its own string library because you have a similarly named string.py and/or string.pyc file in your working directory.

Try deleting both the string.py and string.pyc files and re-run your script.



来源:https://stackoverflow.com/questions/38697915/cannot-figure-out-whats-wrong-with-beautifulsoup4-in-my-python-3-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!