Issue
I installed pip and beautifulsoup4 but I still can't run it.
I checked that the beautifulsoup4 is installed as :
python3 -m pip show beautifulsoup4 Name: beautifulsoup4 Version: 4.12.2 Summary: Screen-scraping library Home-page: Author: Author-email: Leonard Richardson [email protected] License: Location: c:\users\mance\appdata\local\activestate\cache\4e8e4795\Lib\site-packages Requires: soupsieve Required-by: bs4
My Code:
import urllib.request, urllib.parse, urllib.error
from bs4 import BeautifulSoup
url = 'http://py4e-data.dr-chuck.net/comments_42.html' html = urllib.request.urlopen(url).read() soup = BeautifulSoup(html, 'html.parser') ###it says this line is the problem
tags = soup('a') for tag in tags: print ('comments',tag.comments('href', None)) print ('URL:',tag.get('href', None))
result as below:
py ex03-01.py Traceback (most recent call last): File "C:\Users\mance\Documents\Coding\Using Python to Access Web Data\ex03-01.py", line 7, in soup = BeautifulSoup(html, 'html.parser') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\mance\Documents\Coding\Using Python to Access Web Data\bs4_init_.py", line 215, in init self.feed() File "C:\Users\mance\Documents\Coding\Using Python to Access Web Data\bs4_init.py", line 239, in feed self.builder.feed(self.markup) File "C:\Users\mance\Documents\Coding\Using Python to Access Web Data\bs4\builder_htmlparser.py", line 164, in feed parser.feed(markup) File "C:\Users\mance\AppData\Local\Programs\Python\Python311\Lib\html\parser.py", line 110, in feed self.goahead(0) File "C:\Users\mance\AppData\Local\Programs\Python\Python311\Lib\html\parser.py", line 170, in goahead k = self.parse_starttag(i) ^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\mance\AppData\Local\Programs\Python\Python311\Lib\html\parser.py", line 337, in parse_starttag self.handle_starttag(tag, attrs) File "C:\Users\mance\Documents\Coding\Using Python to Access Web Data\bs4\builder_htmlparser.py", line 62, in handle_starttag self.soup.handle_starttag(name, None, None, attr_dict) File "C:\Users\mance\Documents\Coding\Using Python to Access Web Data\bs4_init.py", line 404, in handle_starttag self.currentTag, self._most_recent_element) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\mance\Documents\Coding\Using Python to Access Web Data\bs4\element.py", line 1001, in getattr return self.find(tag) ^^^^^^^^^^^^^^ File "C:\Users\mance\Documents\Coding\Using Python to Access Web Data\bs4\element.py", line 1238, in find l = self.find_all(name, attrs, recursive, text, 1, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\mance\Documents\Coding\Using Python to Access Web Data\bs4\element.py", line 1259, in find_all return self._find_all(name, attrs, text, limit, generator, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\mance\Documents\Coding\Using Python to Access Web Data\bs4\element.py", line 516, in _find_all strainer = SoupStrainer(name, attrs, text, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\mance\Documents\Coding\Using Python to Access Web Data\bs4\element.py", line 1560, in init self.text = self._normalize_search_value(text) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\mance\Documents\Coding\Using Python to Access Web Data\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'
Solution
Update BeautifulSoup4:
python3 -m pip install beautifulsoup4 --upgrade
Downgrade Python: If you don't necessarily need the latest Python version for other purposes, consider using an earlier version like Python 3.8 or 3.9 which might have better compatibility with the libraries and tools you're using.
Answered By - murat taşçı
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.