初学小白,哪位大神能帮忙看一下为什么总是报错呀?
代码:
import urllib.request
from bs4 import BeautifulSoup
url = input('Enter URL: ')
count = int(input('Enter count: '))
position = int(input('Enter position: '))
for i in range(count):
html = urllib.request.urlopen(url).read()
soup = BeautifulSoup(html,'html.parser')
tags = soup('a')
url = tags[position - 1].get('href')
print(url)
报错:
Traceback (most recent call last):
File "/Users/eddietang/PycharmProjects/pythonProject/code3/urllinks.py", line 12, in
soup = BeautifulSoup(html, 'html.parser')
File "/Users/eddietang/PycharmProjects/pythonProject/code3/bs4/__init__.py", line 215, in __init__
self._feed()
File "/Users/eddietang/PycharmProjects/pythonProject/code3/bs4/__init__.py", line 239, in _feed
self.builder.feed(self.markup)
File "/Users/eddietang/PycharmProjects/pythonProject/code3/bs4/builder/_htmlparser.py", line 164, in feed
parser.feed(markup)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/html/parser.py", line 110, in feed
self.goahead(0)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/html/parser.py", line 170, in goahead
k = self.parse_starttag(i)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/html/parser.py", line 344, in parse_starttag
self.handle_starttag(tag, attrs)
File "/Users/eddietang/PycharmProjects/pythonProject/code3/bs4/builder/_htmlparser.py", line 62, in handle_starttag
self.soup.handle_starttag(name, None, None, attr_dict)
File "/Users/eddietang/PycharmProjects/pythonProject/code3/bs4/__init__.py", line 404, in handle_starttag
self.currentTag, self._most_recent_element)
File "/Users/eddietang/PycharmProjects/pythonProject/code3/bs4/element.py", line 1001, in __getattr__
return self.find(tag)
File "/Users/eddietang/PycharmProjects/pythonProject/code3/bs4/element.py", line 1238, in find
l = self.find_all(name, attrs, recursive, text, 1, **kwargs)
File "/Users/eddietang/PycharmProjects/pythonProject/code3/bs4/element.py", line 1259, in find_all
return self._find_all(name, attrs, text, limit, generator, **kwargs)
File "/Users/eddietang/PycharmProjects/pythonProject/code3/bs4/element.py", line 516, in _find_all
strainer = SoupStrainer(name, attrs, text, **kwargs)
File "/Users/eddietang/PycharmProjects/pythonProject/code3/bs4/element.py", line 1560, in __init__
self.text = self._normalize_search_value(text)
File "/Users/eddietang/PycharmProjects/pythonProject/code3/bs4/element.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'



