Skip to content

Instantly share code, notes, and snippets.

@giruzou
Forked from takawo/getNikkeiWebPageTitle.py
Created February 23, 2018 00:58
Show Gist options
  • Save giruzou/de1eb7df4982f96679aca1475259161e to your computer and use it in GitHub Desktop.
Save giruzou/de1eb7df4982f96679aca1475259161e to your computer and use it in GitHub Desktop.
# coding: UTF-8
import urllib.request
from bs4 import BeautifulSoup
# アクセスするURL
url = "http://www.nikkei.com/"
# URLにアクセスする htmlが帰ってくる → <html><head><title>経済、株価、ビジネス、政治のニュース:日経電子版</title></head><body....
html = urllib.request.urlopen(url)
# htmlをBeautifulSoupで扱う
soup = BeautifulSoup(html, "html.parser")
# タイトル要素を取得する → <title>経済、株価、ビジネス、政治のニュース:日経電子版</title>
title_tag = soup.title
# 要素の文字列を取得する → 経済、株価、ビジネス、政治のニュース:日経電子版
title = title_tag.string
# タイトル要素を出力
print(title_tag)
# タイトルを文字列を出力
print(title)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment