Skip to content

Instantly share code, notes, and snippets.

@t0mst0ne
Created April 26, 2016 06:33
Show Gist options
  • Save t0mst0ne/37ea38c0fbd0be681e9d0f5dc964ec8b to your computer and use it in GitHub Desktop.
Save t0mst0ne/37ea38c0fbd0be681e9d0f5dc964ec8b to your computer and use it in GitHub Desktop.
Parse the Taiwan earthquake data and plot
import requests
import pandas as pd
from bs4 import BeautifulSoup
from bokeh.io import output_file, show
from bokeh.models import (
GMapPlot, GMapOptions, ColumnDataSource, Circle, DataRange1d, PanTool, WheelZoomTool, BoxSelectTool
)
# Fetch the data
URL = 'http://scweb.cwb.gov.tw/Page.aspx?ItemId=20&loc=tw&adv=0'
r = requests.get(URL)
# parse the header and data
soup = BeautifulSoup(r.text, "lxml")
viewstate = soup.find('input', {'id' : '__VIEWSTATE'})['value']
eventval = soup.find('input', {'id' : '__EVENTVALIDATION'})['value']
viewgen = soup.find('input', {'id' : '__VIEWSTATEGENERATOR'})['value']
cookies = r.cookies
headers = r.headers
data = {"__VIEWSTATE":viewstate,"__EVENTVALIDATION":eventval, "__VIEWSTATEGENERATOR":viewgen}
data['ctl03$btnSearch'] = '%E9%80%81%E5%87%BA'
DF = pd.DataFrame()
for Y in range (1995,2003):
data['ctl03$ddlYear'] = str(Y)
for M in range(1,13):
data['ctl03$ddlMonth'] = str(M).zfill(2)
r1 = requests.post(URL, data=data)
try:
df = pd.read_html(r1.text,header =0)[1]
df['year'] = Y
DF = pd.concat([DF,df])
print Y,M, r1.status_code
except IndexError: print Y,M,'fail'
for Y in range (2017,2017):
data['ctl03$ddlYear'] = str(Y)
for M in range(1,13):
data['ctl03$ddlMonth'] = str(M).zfill(2)
r1 = requests.post(URL, data=data)
try:
df = pd.read_html(r1.text,header =0)[2]
df['year'] = Y
DF = pd.concat([DF,df])
print Y,M, r1.status_code
except IndexError: print Y,M,'fail'
except ValueError: pass
map_options = GMapOptions(lat=23.8, lng=120.8, map_type="roadmap", zoom=8) #‘satellite’, ‘roadmap’, ‘terrain’, ‘hybrid’
plot = GMapPlot(
x_range=DataRange1d(), y_range=DataRange1d() , map_options=map_options, title="台灣", plot_height = 1000,
plot_width = 1000,
)
source = ColumnDataSource(
data=dict(
lat=DF['緯度'].tolist(),
lon=DF['經度'].tolist(),
)
)
circle = Circle(x="lon", y="lat", size=2, fill_color="red", fill_alpha=0.5, line_color=None)
plot.add_glyph(source, circle)
plot.add_tools(PanTool(), WheelZoomTool(), BoxSelectTool())
output_file("gmap_plot.html")
show(plot)
@mahadev07
Copy link

wow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment