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