Taiwan is an island next to Pacific ocean, and one kind of serious disaster in Taiwan is typhoon. Fortunately, We have best typhoon researches in the world, and I want to make a step further to let people read typhoon predictions on iOS.
There are two main resources that we can get typhoon prediction. One is Central Weather Bureau, Taiwan (CWB), and the other is Joint Typhoon Warning Center (JTWC). Here I’ll focus on CWB data.
Setup inspect environment
At the first scene, CWB seems hard to parse because it uses images to show potential track area instead of using JSON API and google map.
The html code is quite dirty and hard to inspect. The tip is to find the iframe
element from the inspector.
Then, open the direct link in another window.
Now we have a relatively cleaner html to inspect.
Get the information source
I’m lucky. The source is right in JavaScript file.
Though it’s not JSON, but almost (lol). The direct link is
The parameter of the link seems to be a clue that we can query history records, but it doesn’t. With or without the parameter it always gives us the newest information of typhoons.
Parse
We only interested in var typhs= [...]
. It’s easy to get data out using regular
expression.
I tested regular expression with Perl before I actually implement it in Objective-C. If you need a tool to do text processing, Perl is always the best choice. I strongly recommend people to learn Perl, it’s worthy.
1 2 3 4 5 6 7 8 9 10 11 |
|
Beware that not to match greedly, else you won’t get what you want. Now we can write it in much more verbose Objective-C.
1 2 3 4 |
|