Pythonモジュール「beautifulsoup4」でURLからサイトページのタイトルを取得する方法についてソースコード付きでまとめました。
タイトルタグの取得
Pythonモジュール「beautifulsoup4」でURLからサイトページのタイトルを取得します。
サンプルコード(Python3)
サンプルプログラムのソースコードです。
# -*- coding: utf-8 -*- import urllib.request from bs4 import BeautifulSoup # urlのHTMLを取得 url = 'https://algorithm.joho.info/' html = urllib.request.urlopen(url) # htmlをBeautifulSoupでパース soup = BeautifulSoup(html, "html.parser") # タイトル要素の取得 print(soup.title) # <title>アルゴリズム雑記</title> # タイトル要素の文字列を取得 print(soup.title.string) # アルゴリズム雑記
– | 関連記事 |
---|---|
1 | ■【Python/BeautifulSoup4】Webスクレイピング入門 ■Python入門 基本文法 ■【Python】ネットワークプログラミング入門 |
コメント