Skip to content

Instantly share code, notes, and snippets.

@thatseeyou
Created March 22, 2017 04:32
Show Gist options
  • Save thatseeyou/db5127194f462e2400363aeeac83a0f3 to your computer and use it in GitHub Desktop.
Save thatseeyou/db5127194f462e2400363aeeac83a0f3 to your computer and use it in GitHub Desktop.
웹 페이지의 특정 정보를 추출하여 json 형태로 만들기
/*
* This is a JavaScript Scratchpad.
*
* Enter some JavaScript, then Right Click or choose from the Execute Menu:
* 1. Run to evaluate the selected text (Cmd-R),
* 2. Inspect to bring up an Object Inspector on the result (Cmd-I), or,
* 3. Display to insert the result in a comment after the selection. (Cmd-L)
*/
// run at http://www.zdnet.co.kr
/*
<div>
<span class="number">2</span>
<a href="http://www.zdnet.co.kr/news/news_view.asp?artice_id=20170316174010&amp;lo=z45">
<img src="http://image.zdnet.co.kr/2017/03/17/lyk_STS1t7Shx7uuGWZC.jpg" alt="" width="50" height="40">
<span>
IT人은 왜 늘 약자 입장에 처하게 될까
</span>
</a>
</div>
*/
var lis = document.querySelectorAll('.news_9 ol>li');
var result = [];
lis.forEach(function (li) {
var number = li.querySelector('div > span.number').innerText;
var a = li.querySelector('div > a[href]');
var href = a.getAttribute('href');
var img = a.querySelector('img[src]').getAttribute('src');
var span = a.querySelector('span').innerText.trim();
result.push({
'number': number,
'href': href,
'img': img,
'span': span
});
});
JSON.stringify(result);
/*
[{"number":"1","href":"http://www.zdnet.co.kr/news/news_view.asp?artice_id=20170316173706&lo=z45","img":"http://image.zdnet.co.kr/2017/03/17/yong2_f0vIkD3wFSJfWi.jpg","span":"\"IT 개발자?…부품과 다를 바 없어요\""},{"number":"2","href":"http://www.zdnet.co.kr/news/news_view.asp?artice_id=20170316174010&lo=z45","img":"http://image.zdnet.co.kr/2017/03/17/lyk_STS1t7Shx7uuGWZC.jpg","span":"IT人은 왜 늘 약자 입장에 처하게 될까"},{"number":"3","href":"http://www.zdnet.co.kr/news/news_view.asp?artice_id=20170316190727&lo=z45","img":"http://image.zdnet.co.kr/2017/03/16/leespot_O3e2DJZthdZq.jpg","span":"\"4차산업혁명?…IT 中企는 죽을 맛이죠\""},{"number":"4","href":"http://www.zdnet.co.kr/news/news_view.asp?artice_id=20170321104423&lo=z45","img":"http://image.zdnet.co.kr/2017/03/21/iam_28bJKjMVhD1X6rRV.jpg","span":"삼성 갤럭시S8 공개 D-9…기대감 '최고조'"},{"number":"5","href":"http://www.zdnet.co.kr/news/news_view.asp?artice_id=20170320101924&lo=z45","img":"http://image.zdnet.co.kr/2017/03/20/sontech_htwdYxBIzIzL.jpg","span":"일주일 만에 '랩하는 AI' 만든 美 10대 소년"},{"number":"6","href":"http://www.zdnet.co.kr/news/news_view.asp?artice_id=20170320065252&lo=z45","img":"http://image.zdnet.co.kr/2017/03/20/hohocho_X9LALcDp7DKn.jpg","span":"“삼성 갤럭시S8, 세 종류 색상 출시”"},{"number":"7","href":"http://www.zdnet.co.kr/news/news_view.asp?artice_id=20170316183458&lo=z45","img":"http://image.zdnet.co.kr/2017/03/17/lyk_MNDW4CfeIzfLYq3t.jpg","span":"진짜로 혁신하려거든 IT에 부탁하라"},{"number":"8","href":"http://www.zdnet.co.kr/news/news_view.asp?artice_id=20170321063006&lo=z45","img":"http://image.zdnet.co.kr/2017/03/21/iam_8wSei5D7pegaun64.jpg","span":"갤S8에 '빅스비' 버튼…\"인터페이스 혁신\""},{"number":"9","href":"http://www.zdnet.co.kr/news/news_view.asp?artice_id=20170321075310&lo=z45","img":"http://image.zdnet.co.kr/2015/12/09/hohocho_EzcIXw3iD3cd.jpg","span":"전기차 15분 충전…'포르쉐 선언' 가능할까"},{"number":"10","href":"http://www.zdnet.co.kr/news/news_view.asp?artice_id=20170320085016&lo=z45","img":"http://image.zdnet.co.kr/2017/03/20/guyer73_27pY4A56LkVU.jpg","span":"현대차, '2017 아반떼' 출시...1천570만원부터"}]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment