8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

地震が多いので地震の履歴を取得

Last updated at Posted at 2016-04-15

昨日でしょうか、一昨日でしょうか。
大きな地震がありましたね。私はちょうどさっき二回目の地震を体験しました。
そんなことがあり、気象庁のサイトを揺れてる最中閲覧したのですが、特になにも書いてありませんでしたね。忙しかったんでしょうかね。
掲載されたのは地震が終わった数分後でした。

さて、そんな気象庁の地震速報を履歴として取得してみます。
といっても誰でも気象庁のサイトから簡単に見れるんですけどね。
リンク

プログラムは関数getにて履歴を取得、その後引数path(書き込みファイルのパッチ)
へ結果を書き込みます。

このまま実行するとエラーになるか、デスクトップにinfo.textというファイルが作られ書き込まれます。

動作環境は
Python 2.7
OSX 10.10.5

ライブラリは
・urllib2(HTML取得)
・BeautifulSoup4(スクレイピング)
を使用し、
文字コードをutf-8に変換しました。(OSXのみ動作確認)

# coding: utf-8

def get(path):
	import urllib2
	from bs4 import BeautifulSoup

	soup = BeautifulSoup(urllib2.urlopen("http://www.jma.go.jp/jp/quake/quake_sindo_index.html"), "lxml")
	infotable = soup.find_all("div", attrs={"id": "info", "class": "infotable"})
	body = [i.text.encode("utf-8") for i in infotable]

	with open(path, "w") as f:
		f.write("".join(body))

if __name__ == "__main__":
	path = "desktop/info.text"
	get(path)
8
8
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
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?