Ich lerne Python alleine. Ich kenne die Details noch nicht, aber ich werde das Phänomen notieren, über das ich gestolpert bin. Übrigens ist es ** Python 3.8.5 **
Ich habe versucht, das Titel-Tag zu extrahieren.
Code
# html_parser.py
import requests
from bs4 import BeautifulSoup
#URL, die Sie erhalten möchten
url = "http://example.com"
#Holen Sie sich HTML, indem Sie eine HTTP-Anfrage mit der URL als Argument senden
response = requests.get(url)
#Der Zeichencode wird automatisch codiert
response.encoding = response.apparent_encoding
#HTML-Analyse
bs = BeautifulSoup(response.text, 'html.parser')
title_tag = bs.find('title')
#Geben Sie den Textteil des extrahierten Tags aus
print(title_tag.text)
Ich habe einen Importfehler für schöne Suppe.
Ergebnis
Traceback (most recent call last):
File "c:/python/html.py", line 3, in <module>
from bs4 import BeautifulSoup
File "C:\Users\*****\AppData\Local\Programs\Python\Python38\lib\site-packages\bs4\__init__.py", line 31, in <module>
from .builder import builder_registry, ParserRejectedMarkup
File "C:\Users\*****\AppData\Local\Programs\Python\Python38\lib\site-packages\bs4\builder\__init__.py", line 7, in <module>
from bs4.element import (
File "C:\Users\*****\AppData\Local\Programs\Python\Python38\lib\site-packages\bs4\element.py", line 19, in <module>
from bs4.formatter import (
File "C:\Users\*****\AppData\Local\Programs\Python\Python38\lib\site-packages\bs4\formatter.py", line 1, in <module>
from bs4.dammit import EntitySubstitution
File "C:\Users\*****\AppData\Local\Programs\Python\Python38\lib\site-packages\bs4\dammit.py", line 13, in <module>
from html.entities import codepoint2name
File "c:\python\html.py", line 3, in <module>
from bs4 import BeautifulSoup
ImportError: cannot import name 'BeautifulSoup' from partially initialized module 'bs4' (most likely due to a circular import) (C:\Users\*****\AppData\Local\Programs\Python\Python38\lib\site-packages\bs4\__init__.py)
Wenn nicht gesagt, wurde Folgendes getan
python
pip install beautifulsoup
Auch mit Pip-Liste beautifulsoup4 4.9.1 Kann bestätigt werden.
Warum dann ...
Es scheint, dass Python ein Paket namens "html" hat und es geladen hat ...
Recommended Posts