【问题标题】:making web crawler want to extract the images in url让网络爬虫想要提取 url 中的图像
【发布时间】:2013-08-03 04:54:30
【问题描述】:

我想制作网络爬虫,从任何给定的 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 || ''
    } 

【问题讨论】:

    标签: javascript html node.js web-crawler


    【解决方案1】:

    如果$('img').attr('content') 您想将图像本身作为文件下载,那将不起作用,因为图像数据本身是与 HTML 不同的资源,HTML 只是标识图像的 URL。因此,您需要通过其 src 属性值对图像发出 HTTP GET 请求并将其保存为文件。 Node 的核心 http 客户端库以及诸如 requestsuperagent 之类的 npm 模块都可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-18
      • 2015-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-05
      • 1970-01-01
      相关资源
      最近更新 更多