Last active
November 6, 2015 05:34
-
-
Save piyo7/4d79d3af89154305d825 to your computer and use it in GitHub Desktop.
Nightmareで郵便局から住所をスクレイピング ref: http://qiita.com/piyo7/items/d771559c0fb1386f1ffa
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <tr> | |
| <td class="data"><small>100-0001</small></td> | |
| <td class="data"><small>東京都</small></td> | |
| <td class="data"><small>千代田区</small></td> | |
| <td> | |
| <div class="data"> | |
| <p><small><a href="zipcode.php?pref=13&city=1131010&id=46366&merge=">千代田</a></small></p> | |
| <p class="comment"><small>チヨダ</small></p> | |
| </div> | |
| </td> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| > node main.js 1000001 | |
| 〒100-0001 東京都千代田区千代田 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| > node -v | |
| v4.2.1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| var Nightmare = require('nightmare'); | |
| var nightmare = Nightmare(); | |
| Promise.resolve( | |
| nightmare. | |
| goto('http://www.post.japanpost.jp/zipcode/index.html'). | |
| type('input[name="zip"]', process.argv[2]). | |
| click('input[src="/img/zipcode/btn_add_search_s.gif"]'). | |
| wait('.data'). | |
| evaluate(() => { | |
| var data = document.getElementsByClassName('data'); | |
| return { | |
| zip: data[0].innerText, | |
| pref: data[1].innerText, | |
| city: data[2].innerText, | |
| aza: data[3].children[0].innerText | |
| }; | |
| } | |
| ) | |
| ). | |
| then((address) => { | |
| console.log('〒' + address.zip + ' ' + address.pref + address.city + address.aza); | |
| }). | |
| catch(console.error); | |
| nightmare.end(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "dependencies": { | |
| "nightmare": "^2.0.7" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment