본문 바로가기
배우고 있습니다/Node.js 개발

[node.js 활용기] 웹 크롤링 준비 (2) 에러편

by 유헤 2018. 12. 11.

이슈 1.


Casperjs: can't find variable 'a'

https://stackoverflow.com/questions/20870646/casperjs-cant-find-variable-while-jquery-is-injected?rq=1


a 라는 변수가 없다는 것이다. 실제로 

console.log("테스트22" + a);

로 했는데 a 라는 변수를 설정을 안해뒀다...



이슈 2.


casperjs without form

로그인 fill 처럼, form 양식이 없어도 데이터를 가져오는 방법

 = CSS 선택자 가져오기

https://stackoverflow.com/questions/19446232/how-to-fill-up-form-using-casperjs-without-form-tag


document.querySelector("#id_name > .classname > textarea").value = "foo";


이런식으로 querySelector를 이용해야한다. 하위 div의 첫번째를 가져오므로

모든 div 를 다 들고 오고 싶으면 querySelectorAll을 사용하면 됩니다.



이슈 3.


크롤링 하기 위해 검색 TextArea 에 데이터를 넣어줘야 하는 상황.


document.querySelector(~~~).getElementsByTagName('textarea').inputText.value = "데이터";

이런식으로 활용하면 된다.


실제 예제

1
2
3
4
5
6
7
8
casper.then( function(){
    var txt = casper.evaluate( function () {
         document.querySelector('#layout_twodivision > #def_container > .def_content > .search_inputform  > .srch_inputblock ').getElementsByTagName('textarea').inputText.value="car";
 
        return  document.querySelector('#layout_twodivision > #def_container > .def_content > .search_inputform  > .srch_inputblock ').getElementsByTagName('textarea').inputText.value;
    });
    casper.wait(200);
});
cs


크롬 > 마우스 오른쪽 버튼 > 검사

를 활용하면, 실제 CSS 데이터 구조와 변경된 값까지 확인이 가능하니

크롬을 활용하여 테스트 해보면 좋을듯 하다.


# 은 id 값

. 은 class 값

이다.