Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获得一个网站';使用NodeJs将搜索结果转换为JSON_Javascript_Node.js_Express_Request - Fatal编程技术网

Javascript 获得一个网站';使用NodeJs将搜索结果转换为JSON

Javascript 获得一个网站';使用NodeJs将搜索结果转换为JSON,javascript,node.js,express,request,Javascript,Node.js,Express,Request,因为我对Node还不熟悉,所以我尝试使用Node以JSON的形式获取网站搜索结果,我也尝试了http chunk方法和express get,但找不到它。 URL:URLhttps://www.cyccomputer.pe/buscar?search_query=mouse不返回json。所有者呈现一个html页面,并且不提供json 你可以通过刮擦来达到你的目的。您可以使用诸如request、request promise、axios等包获取html,如: const rp = require

因为我对Node还不熟悉,所以我尝试使用Node以JSON的形式获取网站搜索结果,我也尝试了http chunk方法和express get,但找不到它。
URL:

URL
https://www.cyccomputer.pe/buscar?search_query=mouse
不返回
json
。所有者呈现一个
html
页面,并且不提供json

你可以通过刮擦来达到你的目的。您可以使用诸如
request
request promise
axios
等包获取html,如:

const rp = require('request-promise')

rp('https://www.cyccomputer.pe/buscar?search_query=mouse')
  .then(html => console.log(html) // html contains the returned html)

// outputs something like:
<!DOCTYPE HTML>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="es-es"><![endif]-->
<!--[if IE 7]><html class="no-js lt-ie9 lt-ie8 ie7" lang="es-es"><![endif]-->
<!--[if IE 8]><html class="no-js lt-ie9 ie8" lang="es-es"><![endif]-->
<!--[if gt IE 8]> <html class="no-js ie9" lang="es-es"><![endif]-->
<html lang="es-es">
    <head>
...
黏液 //输出 { 节点:'根', 儿童:[ { 节点:'元素', 标签:“div”, 属性:{id:'1',类:'foo'}, 儿童:[ { 节点:'元素', 标签:“h2”, 儿童:[ {node:'text',text:'sample text with'}, {node:'element',tag:'code',child:[{node:'text',text:'inline tag'}]} ] }, ... 更新:(至OP发行)

您可能还想使用
cheerio
包获取html的
正文
,并将其解析为json,如:

const cheerio = require('cheerio');

rp('https://www.cyccomputer.pe/buscar?search_query=mouse')
  .then(html => {
    var data = cheerio.load(html);
    var body = data('body').html();
    var result = html2json(body);
    console.log(result);
  })
  .catch(e => console.log('error', e.message))

注意如果您只是在控制台上进行日志记录,那么对
深度
有一个限制。请查看此项以记录整个对象`

url
https://www.cyccomputer.pe/buscar?search_query=mouse
不返回
json
。所有者呈现
html
页面,并且不提供json

您可以通过抓取来实现您正在尝试的功能。您可以使用诸如
request
request promise
axios
等包来获取html,如:

const rp = require('request-promise')

rp('https://www.cyccomputer.pe/buscar?search_query=mouse')
  .then(html => console.log(html) // html contains the returned html)

// outputs something like:
<!DOCTYPE HTML>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="es-es"><![endif]-->
<!--[if IE 7]><html class="no-js lt-ie9 lt-ie8 ie7" lang="es-es"><![endif]-->
<!--[if IE 8]><html class="no-js lt-ie9 ie8" lang="es-es"><![endif]-->
<!--[if gt IE 8]> <html class="no-js ie9" lang="es-es"><![endif]-->
<html lang="es-es">
    <head>
...
黏液 //输出 { 节点:'根', 儿童:[ { 节点:'元素', 标签:“div”, 属性:{id:'1',类:'foo'}, 儿童:[ { 节点:'元素', 标签:“h2”, 儿童:[ {node:'text',text:'sample text with'}, {node:'element',tag:'code',child:[{node:'text',text:'inline tag'}]} ] }, ... 更新:(至OP发行)

您可能还想使用
cheerio
包获取html的
正文
,并将其解析为json,如:

const cheerio = require('cheerio');

rp('https://www.cyccomputer.pe/buscar?search_query=mouse')
  .then(html => {
    var data = cheerio.load(html);
    var body = data('body').html();
    var result = html2json(body);
    console.log(result);
  })
  .catch(e => console.log('error', e.message))

注意如果你只是简单的控制台日志记录,那么深度就有一个限制。
查看此日志记录整个对象`

你能详细说明你想做什么并分享你的代码吗?我正在尝试制作一个api,使产品在JSONSo中运行。第一步获取页面……第二步,解析页面,第三步,构建并返回json。您需要将其缩小到更小、更具体的问题,并分别处理。然后明确每个较小的子任务中存在的问题。使用HTTP客户端(如axios或request)请求搜索结果页面,然后使用cheerio解析和定位包含信息的节点你需要。只需谷歌“用节点JS抓取网页”你会发现大量的例子。你能详细说明一下你想做什么并分享你的代码吗?我正在尝试制作一个api,让产品在JSONSo中运行。第一步获取页面……第二步,解析页面,第三步,构建并返回json。你需要将其缩小到更小更具体的问题,并处理它们所有这些都是单独进行的。然后明确每个较小的子任务中存在的问题。使用HTTP客户端(如axios)或request来请求搜索结果页面,然后使用cheerio来解析和定位包含所需信息的节点。只需谷歌“使用Node JS进行web scraping”你会发现大量的例子。这些代码对一些网站来说效果很好,但对于我提供的url,它显示“无法读取未定义的属性‘child’”,有没有办法只解析产品结果,因为空搜索有200行JSON输出(找不到产品)首先下载html,浏览
DOM
并找到哪个
div
或元素包含产品,然后您可以获取类似
var product=data('the-product-div').html()
等。该代码对于某些网站很好,但对于我提供的url,它显示“无法读取未定义的属性‘child’”有没有办法只解析产品结果,因为空搜索(找不到产品)有200行JSON输出。首先下载html,遍历
DOM
,找到哪个
div
或元素保存产品,然后你可以抓取像
var product=data('the-product-div')。html()