Created
May 13, 2016 22:18
-
-
Save goodbabies/df0e29d39ed5dce605eebb80d5a87491 to your computer and use it in GitHub Desktop.
This file contains 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
var client = require('cheerio-httpcli'); | |
var request = require('request'); | |
var fs = require('fs'); | |
var URL = require('url'); | |
var Dir = "./files/img"; | |
if (!fs.existsSync(Dir)) { | |
fs.mkdirSync(Dir); | |
} | |
var data = {}; | |
var url = "http://www.pet-home.jp/cats/"; | |
var host = "http://www.pet-home.jp"; | |
var param = {}; | |
client.fetch(url, param, function(err, $, res) { | |
if (err) { console.log("error"); return; } | |
//以下指定要素を繰り返し取得 | |
$(".contribute_result .inner").each(function(i) { | |
//指定要素の中から、さらに取得したい要素を変数に代入していく | |
var src = $(this).find("img:first-child").attr('src'); | |
src = URL.resolve(url, src); | |
var file_name = URL.parse(src).pathname; | |
saveFile = Dir + "/" + file_name.replace(/[^a-zA-Z0-9\.]+/g, '-'); | |
request(src).pipe(fs.createWriteStream(saveFile)); | |
var pet_sex = $(this).find(".pet_sex .petSex_m").text();//改行削除 | |
var pet_sex = pet_sex.replace( /¥n/g , "" ); | |
var pet_link = $(this).find(".title a").attr('href'); | |
console.log(file_name); | |
data[i] = { | |
src: file_name.replace(/[^a-zA-Z0-9\.]+/g, '-'), | |
title: $(this).find("img:first-child").attr('alt'), | |
pet_sub_cate: $(this).find(".pet_sub_cate").text(), | |
pet_sex: pet_sex, | |
pet_area: $(this).find(".pet_area").text(), | |
pet_link: URL.resolve(url, $(this).find(".title a").attr('href')) | |
}; | |
}); | |
fs.writeFile('./files/test.json', JSON.stringify(data, null, ' '), function (err) { | |
if (err) throw err; | |
console.log(data); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment