2013-08-03 17 views
0

나는 추출 된 제목, 설명, 키워드 및 이미지를 추출한 웹 크롤러에서 데이터베이스에 저장하려고합니다. 코드가 작동하지 않습니다. 이미지 ... 어떤 도움을 이해할 수있을 것이다웹 크롤러가 URL에있는 이미지를 추출하고 싶습니다.

var $ = cheerio.load(html); 
    var title = $('head title').text(); 
    var keywords = $('head meta[name=keywords]').attr('content'); 
    var desc = $('head meta[name=description]').attr('content'); 
    var links = $('a'); 
    var img= $('img').attr('content') 
    console.log('Crawling "%s" | %s',title,this.url); 
    async.map(links.map(function(){ 
     var href = $(this).attr('href'); 
     if(href && href != self._url && !(/^#(\w)+/.test(href)) && !util.imageRegexp.test(href)){ 
     if(util.isExternal(href)){ 
     return 'INSERT INTO `queue` SET `id` = \''+util.id()+'\', `url` = '+self.conn.escape(href)+', `from` = '+self.conn.escape(from); 
      console.log("self.conn.escape" + self.conn.escape) 
      } 
      else { 
      return 'INSERT INTO `queue` SET `id` = \''+util.id()+'\', `url` = '+self.conn.escape(util.resolveRelativeURL(href,self._url))+', `from` = '+self.conn.escape(from); 
      } 
      } 
      return false; 
     }).filter(function(el){ 
     return !!el; 
     }) 
     ,this.conn.query.bind(this.conn),function(e,result){ 
     if(e){ 
     console.log('Error writing queue.'); 
     console.log(e); 
     } 
     }); 
    this.conn.query('INSERT INTO `websites` SET ?',{ 
     id:util.id(), 
     url:this.url, 
     from:from, 
     title:title, 
     keywords:keywords || '', 
     img:img || '', 

     desc:desc || '' 
    } 

답변

0

$('img').attr('content')하여 파일로 이미지 자체를 다운로드하려면 이미지 데이터 자체가 HTML에서 별도의 자원이기 때문에, 그것은 작동하지 않을 것이다 단순히 이미지의 URL을 식별합니다. 따라서 이미지의 HTTP GET 요청을 src 속성 값으로 만들어 파일로 저장해야합니다. 노드의 핵심 http 클라이언트 라이브러리가 작동하며, npm 모듈 (예 : request 또는 superagent)도 작동합니다.

관련 문제