Skip to content

Instantly share code, notes, and snippets.

@piyo7
Last active November 6, 2015 05:34
Show Gist options
  • Select an option

  • Save piyo7/4d79d3af89154305d825 to your computer and use it in GitHub Desktop.

Select an option

Save piyo7/4d79d3af89154305d825 to your computer and use it in GitHub Desktop.
Nightmareで郵便局から住所をスクレイピング ref: http://qiita.com/piyo7/items/d771559c0fb1386f1ffa
<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>
> node main.js 1000001
〒100-0001 東京都千代田区千代田
> node -v
v4.2.1
'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();
{
"dependencies": {
"nightmare": "^2.0.7"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment