LoginSignup
2
3

More than 5 years have passed since last update.

BeautifulSoup4 メモ

Posted at
# 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'}]
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3