jQuery访问站点中的DOM

编程入门 行业动态 更新时间:2024-10-22 02:47:02
本文介绍了jQuery访问站点中的DOM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图从这个网站中撷取表格中的各种元素来教导我自己使用node.js,cheerio和请求进行抓取

我无法获得表中的项目,本质上我想获得'rank','company'和'3-从表中可以看出年增长率。我该怎么做?

基于在线 tutorial ,我开发了我的scraping.js脚本,如下所示:

var request = require('request'), cheerio = require('cheerio'); request('www.inc/inc5000/index.html',function(error,response,html){ if(!error&& response.statusCode == 200){ var $ = cheerio.load(html); $('tr.ng-scope')。each(function(i,element){//问题可能在这里$ b $ (b)var a = $(this).get(0); console.log(a); }); } });

然而,我确定我没有在右上方留言。有什么方法可以更好地访问表中的属性?

我注意到Xpaths是这样的

// * [@ id =col-r] / table / tbody / tr 2 / td 1 - rank

// * [@ id =col -r] / table / tbody / tr 2 / td 2 / a - 公司名称 $ b // * [@ id =col -r] / table / tbody / tr 2 a> / td [3] - 3年增长率

试图弄清楚如何访问这些属性据称..

解决方案

您正确的方向。

$()。get()方法返回元素。在你的情况下, var a 是 TR 。这不一定就是你想要的。

你需要做的是将每一行进一步细分为单独的 TD '秒。我使用 $(this).find('td')来做到这一点。然后,我将每个 TD 1加1,然后从中提取文本,将其转换为一个对象,其中键代表表格的字段。所有这些都汇总到一个数组中,但是您可以使用基本概念来构建您认为适合使用的任何数据结构。

request('www.inc/inc5000/index.html',function(error,response,html){ if(error || response.statusCode!= 200)return; var $ = cheerio.load(html); var DATA = []; $ b $('tr.ng-scope')。each(function() { var $ tds = $(this).find('td'); DATA.push({等级:$ tds.eq(0).text( ),公司:$ tds.eq(1).text(),增长:$ tds.eq(2).text(),收入:$ tds.eq(3) ).text(), industry:$ tds.eq(4).text()}); }); console.log(DATA ); });

I am trying to scrape various elements in a table from this site to teach myself scraping using node.js, cheerio and request

I have trouble getting the items in the table, essentially I want to get 'rank','company' and '3-year growth' from the table. How do I do this?

Based on an online tutorial, I have developed my scraping.js script to look like this:

var request = require ('request'), cheerio = require ('cheerio'); request('www.inc/inc5000/index.html', function (error, response, html) { if (!error && response.statusCode == 200) { var $ = cheerio.load(html); $('tr.ng-scope').each(function(i, element){ //problem probably lies here var a = $(this).get(0); console.log(a); }); } });

However, I am sure I am not getting the line with comment above right. Is there a way I can access the attributes in the table better?

I notice the Xpaths are as such

//*[@id="col-r"]/table/tbody/tr2/td1 -- rank

//*[@id="col-r"]/table/tbody/tr2/td2/a -- name of company

//*[@id="col-r"]/table/tbody/tr2/td[3] -- 3 year growth rate

Just trying to figure out how to access these attributes accordingly..

解决方案

You're on the right track.

The $().get() method returns the element. In your case var a is the TR. That's not necessarily what you want.

What you need to do is further subdivide each row into the individual TD's. I did this using $(this).find('td'). Then, I grab each TD 1 by 1 and extract the text out of it, converting that into an object where the key represents the field of the table. All of these are aggregated into an array, but you can use the basic concept to build whatever data structure you see fit to utilize.

request('www.inc/inc5000/index.html', function (error, response, html) { if(error || response.statusCode != 200) return; var $ = cheerio.load(html); var DATA = []; $('tr.ng-scope').each(function(){ var $tds = $(this).find('td'); DATA.push({ rank: $tds.eq(0).text(), company: $tds.eq(1).text(), growth: $tds.eq(2).text(), revenue: $tds.eq(3).text(), industry: $tds.eq(4).text() }); }); console.log(DATA); });

更多推荐

jQuery访问站点中的DOM

本文发布于:2023-11-28 13:12:32,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:站点   jQuery   DOM

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!