[PYTHON] Since there are many earthquakes, get the history of earthquakes

Is it yesterday or the day before yesterday? There was a big earthquake. I just experienced the second earthquake. Because of that, I browsed the Japan Meteorological Agency's website while it was shaking, but I didn't write anything in particular. I wonder if he was busy. It was posted a few minutes after the quake ended.

Now, let's get the earthquake early warning of the Japan Meteorological Agency as a history. Anyone can easily see it from the Japan Meteorological Agency website. Link

The program gets the history with the function get, and then the argument path (patch of the write file) Write the result to.

If you execute it as it is, an error will occur or a file called info.text will be created and written to the desktop.

The operating environment is Python 2.7 OSX 10.10.5

The library is ・ Urllib2 (HTML acquisition) ・ Beautiful Soup4 (scraping) Using, I converted the character code to utf-8. (Check operation only on 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)

Recommended Posts

Since there are many earthquakes, get the history of earthquakes
Get the absolute path of the script you are running
How many nodes are there?
Get the number of digits
Get the number of views of Qiita
Get the attributes of an object
Get the first element of queryset
Get the number of Youtube subscribers
Get the column list & data list of CASTable
Scraping the usage history of the community cycle
Get the minutes of the Diet via API
Get the value of the middle layer of NN
Get the last day of the specified month
[Python] Get the character code of the file
Get the filename of a directory (glob)
[PowerShell] Get the reading of the character string
[linux test command] Check the existence of the directory. Used to see if there are too many directories to specify
Get the contents of git diff from python
Check the type of the variable you are using
The contents of the Python tutorial (Chapter 5) are itemized.
The contents of the Python tutorial (Chapter 4) are itemized.
[Python] Get / edit the scale label of the figure
The contents of the Python tutorial (Chapter 2) are itemized.
[Python] Get the main topics of Yahoo News
Get the caller of a function in Python
The contents of the Python tutorial (Chapter 8) are itemized.
The contents of the Python tutorial (Chapter 1) are itemized.
The contents of the Python tutorial (Chapter 10) are itemized.
Determining if there are birds in the image
[Python] Get the last updated date of the website
Get only the address part of NIC (eth0)
The contents of the Python tutorial (Chapter 6) are itemized.
To get the path of the currently running python.exe
The contents of the Python tutorial (Chapter 3) are itemized.
[Python] Get the day of the week (English & Japanese)
Get the update date of the Python memo file.