[PYTHON] BeautifulSoup4 Memo

# list.html
<html>
  <head><title></title></head>
  <body>
    <a href="http://www.example.com/index.html" title="link title a">Example A</a>
    <a href="http://wwww.example.org/" title="link title b" target="_blank">Example B</a>
    <a href="http://www.example.net/" title="link title c">Example C</a>
  </body>
</html>
from bs4 import BeautifulSoup

soup = BeautifulSoup(open("list.html"))

link = soup.find("a")
print(link["title"])
# link title a
print(link["href"])
# http://www.example.com/index.html
print(link.string)
# Example A

link = soup.find("a", target="_blank")
print(link.string)
# Example B
print(link["title"])
# link title b
print(link["href"])
# http://wwww.example.org/

i = [ {"title": x["title"], "url": x["href"], "content": x.string } for x in soup.find_all("a")]
print(i)
# [{'content': 'Example A', 'url': 'http://www.example.com/index.html', 'title': 'link title a'}, {'content': 'Example B', 'url': 'http://wwww.example.org/', 'title': 'link title b'}, {'content': 'Example C', 'url': 'http://www.example.net/', 'title': 'link title c'}]

Recommended Posts

BeautifulSoup4 Memo
Himbeer-Pi-Memo
Pandas Memo
HackerRank-Memo
Python-Memo
Graphen-Memo
Kolben Memo
Matplotlib-Memo
pytest memo
sed memo
Python-Memo
Installieren Sie Memo
networkx memo
Python-Memo
Kater Memo
Befehlsnotiz
Generator Memo.
psycopg2 memo
Python-Memo
SSH-Memo
Notiz: rtl8812
Pandas Memo
Shell Memo
Python-Memo
Pycharm-Memo
[Memo] Verwendung von BeautifulSoup4 (1) HTML anzeigen
Selen, Phantomjs & BeautifulSoup4
AtCoder Andachtsnotiz (11/12)
[OpenCV] Persönliches Memo
PyPI-Push-Memo
LPIC201 Studiennotiz
Jupyter Notizbuch Memo
LPIC304 Virtualisierungsnotiz
ALDA-Ausführungsnotiz
Python-Anfänger-Memo (9.2-10)
youtube download memo
Inu x Memo
Django Lernnotiz
LPIC101 Studiennotiz
Python-Anfänger-Memo (9.1)
Linux (Ubuntu) Memo
scp befehlsnotiz
Kolbengrundierung Memo
Sellerie / Kombu Memo
Wer befiehlt Memo
Django Tutorial Memo
Flask Grundnotiz
Linux # Command Memo 1
★ Memo ★ Python Iroha
Memo zur Geschlechtserkennung
Bildlesememo
[MEMO] [TERMINAL] Alacritty
Notiz der 3D-Rotation (1)
[Python] EDA-Memo
Python 3-Operator-Memo
H2O.ai Einführungsnotiz
Lambda-Ausdrucksnotiz
[Memo] [Terminal] xfce-Terminal