栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

报错 soup = BeautifulSoup(html, ‘html.parser‘)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

报错 soup = BeautifulSoup(html, ‘html.parser‘)

初学小白,哪位大神能帮忙看一下为什么总是报错呀?

代码:

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'

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/893502.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号