Nodejs 请教node-jquery的用法?

Nodejs 请教node-jquery的用法?

var $ = require(‘jquery’); $.get(“http://www.windeln.de/hipp-milchnahrung-combiotik.html”,function(html){ var $doc = $(html); $doc.find(".clickable-tr").each(function(project){ var $project = $(project); console.log($project);

输出project,总是空行,为何?


6 回复

当然可以!针对你提出的问题,让我们来详细探讨一下 node-jquery 的用法,并解决你在代码中遇到的问题。

问题分析

首先,node-jquery 并不是一个标准的 Node.js 包。jQuery 主要是为浏览器环境设计的,用于处理 DOM 操作。而在 Node.js 环境中,由于没有 DOM,直接使用 jQuery 可能会有一些限制或不兼容的情况。

解决方案

为了在 Node.js 中实现类似的功能,我们可以使用一些替代库,比如 cheeriocheerio 是一个轻量级的、类似于 jQuery 的库,专门用于解析 HTML 文档并进行操作。它非常适合用于服务器端处理 HTML 数据。

示例代码

下面是一个使用 cheerio 来解析和处理 HTML 的示例代码:

const axios = require('axios'); // 用于发起 HTTP 请求
const cheerio = require('cheerio'); // 类似 jQuery 的库,用于解析 HTML

// 发起 HTTP GET 请求获取网页内容
axios.get("http://www.windeln.de/hipp-milchnahrung-combiotik.html")
  .then(response => {
    const html = response.data; // 获取返回的 HTML 内容

    // 使用 cheerio 加载 HTML
    const $ = cheerio.load(html);

    // 查找所有 class 为 "clickable-tr" 的元素
    $(".clickable-tr").each((index, element) => {
      const project = $(element);
      console.log(project.html()); // 输出每个找到的元素的 HTML 内容
    });
  })
  .catch(error => {
    console.error(`请求错误: ${error}`);
  });

解释

  1. 引入依赖

    • axios:用于发起 HTTP 请求。
    • cheerio:用于解析 HTML 并进行类似 jQuery 的操作。
  2. 发起 HTTP 请求

    • 使用 axios.get() 方法获取指定 URL 的 HTML 内容。
  3. 加载 HTML

    • 使用 cheerio.load() 方法加载获取到的 HTML 内容。
  4. 查找并处理元素

    • 使用 $(".clickable-tr") 查找所有 class 为 clickable-tr 的元素。
    • 使用 .each() 方法遍历这些元素,并打印出每个元素的 HTML 内容。

通过这种方式,你可以更有效地在 Node.js 中处理 HTML 数据。希望这能帮助你解决问题!


用jsdom 比node-jquery 强。。

也用 jsdom

这代码跑不起来的吧, 花括号都没全… 都看不出来怎么执行的

来,上完整代码,解决了!

var $ = require(‘jquery’); var http = require(‘http’); var options = { host: ‘www.windeln.de’, port: 80, path: ‘/hipp-milchnahrung-combiotik.html’, headers: {‘user-agent’: ‘Mozilla/5.0’} };

var html = ‘’; http.get(options, function(res) { res.on(‘data’, function(data) { // collect the data chunks to the variable named "html" html += data; }).on(‘end’, function() { // the whole of webpage data has been collected. parsing time! var item = $(html).find(‘tr.clickable-tr’).each(function($this){

		var product_inventory=$(this).find("td.number").find("div#sp-stock_qty").text();
    var product_group=$(this).find('td.group').text();
    var product_desc=$(this).find("td.desc").text();
	var product_info=$(this).find("td.description").find("b").text();
	var product_price=$(this).find("td.price").find("div.price-box").find("span.regular-price").find("span.actualPrice").find("span.price").text();

    
    console.log("---------------------------------------------------");
    console.log("分组:"+product_group);
    console.log("产品描述:"+product_desc);
    console.log("产品全名:"+product_info);
    console.log("价格"+product_price);
});

});

});

node-jquery 并不是一个标准的 Node.js 模块,jQuery 是一个设计给浏览器环境使用的库,主要用于操作 DOM 和处理 AJAX 请求。在 Node.js 中直接使用 jQuery 是不可行的,因为 Node.js 环境中没有浏览器提供的 DOM API。

如果你想在 Node.js 中实现类似的功能,可以考虑使用 axios 来发送 HTTP 请求,并使用 cheerio 来解析 HTML。下面是一个使用 axioscheerio 的示例:

示例代码

const axios = require('axios');
const cheerio = require('cheerio');

axios.get("http://www.windeln.de/hipp-milchnahrung-combiotik.html")
    .then(response => {
        const html = response.data;
        const $ = cheerio.load(html);

        $('.clickable-tr').each((index, element) => {
            const project = $(element);
            console.log(project.text());
        });
    })
    .catch(error => {
        console.error('Error fetching the webpage:', error);
    });

解释

  1. 引入依赖

    • axios 用于发送 HTTP 请求。
    • cheerio 用于解析 HTML,类似于 jQuery 在浏览器中的功能。
  2. 发起请求

    • 使用 axios.get 方法发起 GET 请求,获取网页的 HTML 内容。
  3. 解析 HTML

    • 使用 cheerio.load 方法将返回的 HTML 字符串加载到 cheerio 中,形成可操作的 DOM 结构。
  4. 遍历元素

    • 使用 .each() 方法遍历具有 clickable-tr 类的每一个元素。
    • 将当前元素包装成一个 jQuery 对象,通过 $(element) 实现。
    • 使用 console.log(project.text()) 打印出当前元素的文本内容。

通过这种方式,你可以实现与 jQuery 类似的 DOM 操作功能。

回到顶部