第一次尝试时的美丽汤追溯



你好,我是python和Beautiful Soup的新手。我已经下载了带有pip安装的BS4,并正在尝试进行一些网页浏览。我已经看了很多帮助指南,但还没能让BeautifulSoup((通过cmd编译器工作。这是我的代码:

import urllib.request, urllib.parse, urllib.error
from bs4 import BeautifulSoup
import ssl
# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
url = input('Enter - ')
html = urllib.request.urlopen(url, context=ctx).read()
soup = BeautifulSoup(html, 'html.parser')
# Retrieve all of the anchor tags
tags = soup('a')
for tag in tags:
print(tag.get('href', None))

这是我通过URL输入得到的回溯:

C:UsersaaronOneDriveDesktopCoding>python urllinks_get.py
Enter - http://www.dr-chuck.com/page1.htm
Traceback (most recent call last):
File "C:UsersaaronOneDriveDesktopCodingurllinks_get.py", line 21, in <module>
soup = BeautifulSoup(html, 'html.parser')
File "C:UsersaaronOneDriveDesktopCodingbs4__init__.py", line 215, in __init__
self._feed()
File "C:UsersaaronOneDriveDesktopCodingbs4__init__.py", line 239, in _feed
self.builder.feed(self.markup)
File "C:UsersaaronOneDriveDesktopCodingbs4builder_htmlparser.py", line 164, in feed
parser.feed(markup)
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2032.0_x64__qbz5n2kfra8p0libhtmlparser.py", line 110, in feed
self.goahead(0)
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2032.0_x64__qbz5n2kfra8p0libhtmlparser.py", line 170, in goahead
k = self.parse_starttag(i)
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2032.0_x64__qbz5n2kfra8p0libhtmlparser.py", line 344, in parse_starttag
self.handle_starttag(tag, attrs)
File "C:UsersaaronOneDriveDesktopCodingbs4builder_htmlparser.py", line 62, in handle_starttag
self.soup.handle_starttag(name, None, None, attr_dict)
File "C:UsersaaronOneDriveDesktopCodingbs4__init__.py", line 404, in handle_starttag
self.currentTag, self._most_recent_element)
File "C:UsersaaronOneDriveDesktopCodingbs4element.py", line 1001, in __getattr__
return self.find(tag)
File "C:UsersaaronOneDriveDesktopCodingbs4element.py", line 1238, in find
l = self.find_all(name, attrs, recursive, text, 1, **kwargs)
File "C:UsersaaronOneDriveDesktopCodingbs4element.py", line 1259, in find_all
return self._find_all(name, attrs, text, limit, generator, **kwargs)
File "C:UsersaaronOneDriveDesktopCodingbs4element.py", line 516, in _find_all
strainer = SoupStrainer(name, attrs, text, **kwargs)
File "C:UsersaaronOneDriveDesktopCodingbs4element.py", line 1560, in __init__
self.text = self._normalize_search_value(text)
File "C:UsersaaronOneDriveDesktopCodingbs4element.py", line 1565, in _normalize_search_value
if (isinstance(value, str) or isinstance(value, collections.Callable) or hasattr(value, 'match')
AttributeError: module 'collections' has no attribute 'Callable'

真的很想继续我的在线课程,所以任何帮助都将不胜感激!

谢谢!

发现我的问题。我已经安装了beautifulsoup4,并在程序运行的同一目录下使用了bs4文件夹。我没有意识到它们会相互干扰。当我从目录中删除bs4文件夹时,我的程序运行良好:(

最新更新